这是代码:
#coding=utf-8
from apscheduler.scheduler import Scheduler
import logging
logging.basicConfig(filename='/tmp/log', level=logging.DEBUG,
format='%(levelname)s[%(asctime)s]: %(message)s')
sched = Scheduler()
sched.start()
#@sched.interval_schedule(seconds=3)
def job_function():
logging.debug('hello world')
sched.add_interval_job(job_function, seconds=3)
如果我切换到装饰器,仍然无法正常工作。日志是这样的:
DEBUG[2011-10-09 11:02:45,175]: Looking for jobs to run
DEBUG[2011-10-09 11:02:45,176]: No jobs; waiting until a job is added
INFO[2011-10-09 11:02:45,176]: Added job "job_function (trigger: interval[0:00:03], next run at: 2011-10-09 11:02:48.176444)" to job store "default"
INFO[2011-10-09 11:02:45,177]: Shutting down thread pool
添加了作业job_function
,但却触发了神经,为什么?
答案 0 :(得分:4)
如果这是您的所有代码,那么很明显为什么它不起作用 - 应用程序在计划执行作业之前退出。请参阅https://bitbucket.org/agronholm/apscheduler/src/tip/examples提供的示例。
答案 1 :(得分:3)
如documentation中所述,如果您希望阻止调度程序,则需要将standalone
标记设置为True
。
s = Scheduler(standalone=True)
<add jobs here>
s.start()
确保添加信号处理程序或捕获中断异常: - )