Marvin Noll
Marvin Noll

Reputation: 675

Start python apscheduler at next full second

I've tested the APscheduler with the provieded example:

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()

The output:

Tick! The time is: 2020-09-14 00:56:23.225999
Tick! The time is: 2020-09-14 00:56:28.226864
...

What is the best way to start the scheduler at the next full second/the next full 10s/next full minute/... so the output is more readable?

Example output, when it starts at the next round second:

Tick! The time is: 2020-09-14 00:56:23.000000
Tick! The time is: 2020-09-14 00:56:28.000000
...

Upvotes: 1

Views: 573

Answers (1)

Aethernet
Aethernet

Reputation: 21

Referring to the documentation, you could set a start-date:

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')

Upvotes: 2

Related Questions