Avec pip ou easy_install.
$ easy_install apscheduler
Ou
$ pip install apscheduler
import apscheduler.scheduler as ApSched
def job_function():
print "Hello World"
sched = ApSched.Scheduler(standalone=True,coalesce=True)
sched.add_interval_job(job_function, seconds=10)
sched.start()
L'option autonome est False par défaut. S'il est défini sur True, le planificateur lui-même fonctionnera démoniaque.
Le résultat de l'essayer avec ipython.
In [18]: 2013-11-26 16:12:32.680493
Hello World
In [18]: 2013-11-26 16:12:42.680962
Hello World
In [18]: 2013-11-26 16:12:52.680975
Hello World
De plus, le temps d'intervalle est calculé en omettant correctement le temps d'exécution du programme.
from apscheduler.scheduler import Scheduler
import datetime
import time
def job_function_13sec():
print datetime.datetime.today()
time.sleep(13)
print "Hello World"
sched = Scheduler(standalone=True,coalesce=True)
sched.add_interval_job(job_function_13sec, seconds=10)
sched.start()
résultat.
In [46]: sched.start()
2013-11-26 16:36:10.818178
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:36:30.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:36:30.818776
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:36:40.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:36:50.818992
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:37:10.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:37:10.818815
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:37:30.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:37:30.818828
En tant qu'option de travail __ Par défaut, max_run est 1__, ce qui empêche les lancements multiples.
Recommended Posts