r/pythontips • u/Gregpahl97 • Jul 06 '24
Python3_Specific Need a scheduler that is precise
I need something to execute my python code EXACTLY at a specified time. Ideally not even a ms late. I tried heroku scheduler and it was too slow, was thinking maybe using a cron job but that is probably too slow. What am I missing? Any recommendations to a scheduler that is extremely precise would be much appreciated.
4
Upvotes
10
u/DataDoctorX Jul 06 '24
I'm extremely curious about the application here... what's the use case that requires such precision?
3
u/Gerard_Mansoif67 Jul 06 '24
This kind of precision won't be easily done without additional hardware. At work we have timers boards on PCIe which trigger and interrupt after a defined time, so we can use them.
But even in that case you're restrained under the OS management. For Windows 7 and more you can get delay before entering the Interrupt routines that may be up to 50ms (was a great issue when you want to test the timer). Windows XP wasn't that unpredictable (but far far less secure). Don't know about Linux.
You were talking about cron, I don't think this may be enough précise, even less precise than time.sleep(xxx) structure.
Actually I may see one solution which is crap : A while True loop that see the time.time() value and when it's x.0010000 you start start the process (which shall be in another thread with an Event for example). But fetching the exact value will be impossible since you cannot predict exactly how fast Python will execute and how Windows Scheduler will behave
1
u/pblokhout Jul 06 '24
If precision is in microseconds, Python is the wrong language. Even whether you run it once or twice will be many microseconds difference.
2
u/dasandaz Jul 06 '24
Try APScheduler
2
u/Arslanmuzammil Jul 06 '24
Try airflow with cron