我已经调整了这个基本示例以说明主题中的要点: https://github.com/agronholm/apscheduler/blob/master/examples/executors/processpool.py
这是经过调整的代码(请参见args = [datetime.now()])
#!/usr/bin/env python
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler
def tick(param):
print('Tick! The time is: %s' % param)
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_executor('processpool')
scheduler.add_job(tick, 'interval', seconds=3, args=[datetime.now()])
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
当我运行它时,输出时间戳不会更新:
$ ./test.py
Press Ctrl+C to exit
Tick! The time is: 2019-01-28 19:41:53.131599
Tick! The time is: 2019-01-28 19:41:53.131599
Tick! The time is: 2019-01-28 19:41:53.131599
这是预期的行为吗?我使用的是Python 3.6.7和apscheduler 3.5.3,谢谢。
答案 0 :(得分:0)
这与APScheduler无关。您正在做的事情可以这样重写:
args = [datetime.now()]
scheduler.add_job(tick, 'interval', seconds=3, args=args)
您要呼叫datetime.now()
,然后将其返回值在列表中传递给scheduler.add_job()
。由于您传递的是日期时间,您如何期望APScheduler每次执行目标函数时都调用datetime.now()
?