0

I want to run the program at the exact start of a minute (eg. 1:30:00, 1:20:00, 1:19:00 [Hour,minute,seconds]) There are many similar questions but they do not have the case in which the program is linked to a defined time, not just 60 second intervals. Other solutions are in other languages. Is there any python solution to this problem?

Victor Sim
  • 358
  • 1
  • 13

2 Answers2

1

There are several options available. Here are three that I know of:

  1. You can build your own solution using the sched package in the Python standard library: https://docs.python.org/3/library/sched.html
  2. There is a third-party package called schedule, with the periodic functionality that you requested: https://schedule.readthedocs.io/en/stable/
  3. As suggested in the comments, the Celery task queue library has a component called "Celery beat", which you can use to schedule periodic tasks: https://docs.celeryproject.org/en/stable/userguide/periodic-tasks.html#guide-beat

Note that the first two libraries are not inherently multi-threaded. I'm not sure exactly what happens if a job blocks the main thread and overlaps the next scheduled run. You will need to consult the documentation for that, or ensure that all your jobs run in a non-blocking fashion.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
1

Then I guess the best you can do is to combine the answer here: Python threading.timer - repeat function every 'n' seconds

but change the wait condition based on current time:

from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)

when you create your thread compute how much time it has to wait before it gets executed. Then it should within miliseconds precision start at the designated time.

Cheers