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
#1
I'm not sure if this is the correct area, so I apologize, anyways, I'm new to learning python, for the past 2 days I've been looking for an answer to this. I want to add code that can make it so it pauses the script (not ending it) and then at an exact time it will run the few lines below it. Such as if I wanted it to pause it until 10:40 pm, I need it to be accurate to the very millisecond. Example: 2021 2 21 22 40 00 00 Year month day hour minute(s) second(s) millisecond(s)

The closest code to it that I got didn't do it accurately and most of the time had a second or more delay.
---The code
import sched
import time as time_module
def myfunc(): print("test123")

scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2021-02-21 22:25:14', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
scheduler_e = scheduler.enterabs(t, 1, myfunc, ())
scheduler.run()
---
I put the rest of my code here.
Reply
#2
I'm not sure if this is the correct area, so I apologize, anyways, I'm new to learning python, for the past 2 days I've been looking for an answer to this. I want to add code that can make it so it pauses the script (not ending it) and then at an exact time it will run the few lines below it. Such as if I wanted it to pause it until 10:40 pm, I need it to be accurate to the very millisecond. Example: 2021 2 21 22 40 00 00 Year month day hour minute(s) second(s) millisecond(s)

The closest code to it that I got didn't do it accurately and most of the time had a second or more delay.
---The code
import sched
import time as time_module
def myfunc(): print("test123")

scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2021-02-21 22:25:14', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
scheduler_e = scheduler.enterabs(t, 1, myfunc, ())
scheduler.run()
---
I put the rest of my code here.
Reply
#3
Some machines/operating systems may not schedule your process every millisecond (especially under load). So anything you do in python is dependent on the OS running your program "quickly" enough. (time.time_ns() is preferred over time.time())

But what I'd probably do to get fairly accurate while not spinning on a CPU constantly is to sleep() until a few seconds prior to the time you want. Then calculate what the target time will be in epoch time and spin in a loop until it passes.
Reply
#4
You can expect the time to be accurate if you synchronize with some atomic clock (with some time server) BUT there might be a delay of 0.02 to 0.1 second if you are unlucky. The delay depends on network activities and scheduled computer activities with high priority. Even if you use ns precision these delays can't be calculated by any means. The method that @bowlofred suggests is OK but even sleep(n) is to be considered "sleep at least for n seconds". There might be an unlucky high priority activity scheduled at that precise moment. I wouldn't expect a higher precision than 0.1 sec.
Reply
#5
Out of curiosity, what are you doing that is so time sensitive?
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 546 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 345 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 480 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 971 Aug-31-2023, 05:36 PM
Last Post: deanhystad
Question Running an action only if time condition is met alexbca 5 1,261 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Code Assistance needed in saving the file MithunT 0 783 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Regular Expression search to comment lines of code Gman2233 5 1,593 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Code running many times nad not just one? korenron 4 1,325 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,548 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,421 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