Python Forum
Assistance with running a few lines of code at an EXACT time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assistance with running a few lines of code at an EXACT time
#6
If you're not using a real-time operating system, then no language can guarantee it'll be running at a specific time. Normally (99% of the time), being "close enough" is fine. But those times you really do need extremely specific timing without any delays, it's incredibly important, like for pacemakers (offset a heart beat a bit, and you might be killing someone).

So first, you should define how important it actually is to be that accurate. If it truly is that important, you probably shouldn't be using python at all.

But if it isn't critical, time.sleep() is fine:
import time

# 1 min = 60 sec
# 1 sec = 1e9 ns
delay_for = 60 * 1e9
print("Starting...")
delay_until = time.time_ns() + delay_for
while delay_until > time.time_ns():
    time.sleep((delay_until - time.time_ns()) / 1e9)
print("1 minute(ish) has passed")
Reply


Messages In This Thread
RE: Assistance with running a few lines of code at an EXACT time - by nilamo - Feb-24-2021, 10:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 723 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 436 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 571 Nov-10-2023, 08:09 AM
Last Post: akbarza
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,108 Aug-31-2023, 05:36 PM
Last Post: deanhystad
Question Running an action only if time condition is met alexbca 5 1,382 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Code Assistance needed in saving the file MithunT 0 846 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Regular Expression search to comment lines of code Gman2233 5 1,738 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Code running many times nad not just one? korenron 4 1,408 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,864 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,557 Apr-06-2022, 03:41 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020