在下一整秒启动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')

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...