我正在尝试为 Github 上的一个项目做出贡献,以收集资金
数据。
代码..
# time_keeper.py
from apscheduler.scheduler import Scheduler
class TimeKeeper:
def __init__(self):
self.sched = Scheduler()
def queue_job(self):
print("message send to queue")
def start_timers(self):
self.sched.start()
self.sched.add_cron_job(self.queue_job, minute='0-59')
if __name__ == "__main__":
from time_keeper import TimeKeeper
TimeKeeper().start_timers()
问题是,一旦脚本被执行,它就会运行一瞬间然后停止,没有回溯错误。
该函数是否被错误调用或者我遗漏了代码的某些部分?非常感谢社区的帮助!
您的问题的正式答案是,当使用 APScheduler v2 时,调度程序的默认行为是以线程模式运行,该模式将在您应用后立即返回.start()
:
https://github.com/agronholm/apscheduler/blob/2.1/apscheduler/scheduler.py#L90-L91 https://github.com/agronholm/apscheduler/blob/2.1/apscheduler/scheduler.py#L90-L91
由于它立即返回并且没有任何东西使程序的主线程保持活动状态,因此您的程序立即退出。您需要让您的程序运行足够长的时间,以便调度程序可以触发事件,或者您需要使用调度程序的阻塞版本来运行。
对于这个旧版本的 APscheduler,如果您希望调度程序阻塞,则需要以独立模式运行:
https://github.com/agronholm/apscheduler/blob/2.1/examples/interval.py https://github.com/agronholm/apscheduler/blob/2.1/examples/interval.py
或者如果您想继续在线程模式下运行:
https://github.com/agronholm/apscheduler/blob/2.1/examples/threaded.py https://github.com/agronholm/apscheduler/blob/2.1/examples/threaded.py
新版本的 APScheduler 有单独的BlockingScheduler and
BackgroundScheduler` 类,您应该查阅更新的 API 的适当示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)