问题描述
我已经通过以下示例测试了APscheduler:
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.Now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(tick,'interval',seconds=3)
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2)
except (KeyboardInterrupt,SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
输出:
Tick! The time is: 2020-09-14 00:56:23.225999
Tick! The time is: 2020-09-14 00:56:28.226864
...
在下一整秒/下一整十秒/下一整分钟/ ...启动调度程序的最佳方法是什么?
示例输出,从下一秒开始:
Tick! The time is: 2020-09-14 00:56:23.000000
Tick! The time is: 2020-09-14 00:56:28.000000
...
解决方法
参考文档,您可以设置开始日期:
https://apscheduler.readthedocs.io/en/stable/modules/triggers/interval.html
scheduler.add_job(tick,'interval',seconds=3,start_date='2021-06-23 11:36:00',end_date='2021-06-23 11:37:00')