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?
Asked
Active
Viewed 43 times
0
-
Check Celery - https://docs.celeryproject.org/en/stable/getting-started/introduction.html – Tom Ron Jun 11 '20 at 11:58
-
You mean for you program to be executed at a specific time? Like from a scheduler? Or a process in your program to start at a specific time? – Theofrastos Mantadelis Jun 11 '20 at 12:00
-
For the part of the program to be run at a specific time. – Victor Sim Jun 11 '20 at 12:01
2 Answers
1
There are several options available. Here are three that I know of:
- You can build your own solution using the
sched
package in the Python standard library: https://docs.python.org/3/library/sched.html - There is a third-party package called
schedule
, with the periodic functionality that you requested: https://schedule.readthedocs.io/en/stable/ - 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

Theofrastos Mantadelis
- 157
- 7