如何解决在下一整秒启动python apscheduler
我已经通过以下示例测试了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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。