1

So I have read the documentation, articles about the different clocks (monotonic, time_perf, process_time) accessible from Python, and I am still getting unacceptable delays when calling a function using a scheduler. I am getting delays of 12 to 30 seconds.

def schedule_function():
    #recording time when function is called and printing
    print(datetime.datetime.now())


current_time = datetime.datetime.now()
print("The current time on this computer is:", current_time)

# -- some code where I calculate the delay in seconds as a float called 'seconds' ---

#Scheduled event
s = sched.scheduler(time.monotonic, time.sleep) 
s.enter(seconds, 0, schedule_function)
#where seconds is delay in seconds calculated from user user input datetime
s.run()

My output looks something like this: The current time on this computer is: 2023-05-02 16:11:36.710999

Please the enter the time which you would like the function to run today: <2023-05-02 16:12:00>

2023-05-02 16:12:11.147581

The calculations themselves take a negligible amount of time. I don't know enough about how the scheduler calls delayfunc() to understand if there is anyway to mitigate this using the scheduler function.

Should I just use CRON?

I just wanted to know if I was doing something incorrectly or misusing before just jumping to a different method to solve.


I have ensured that there were no delays created elsewhere and tried setting the time.sleep to a smaller increment (which generates a " 'NoneType' object is not callable" error). I have tried using different clocks with higher resolution / tick rates (perf_counter and process_time give me worse results - Why?) but even 64 ticks shouldn't result in a second delay let alone 12. https://www.webucator.com/article/python-clocks-explained/

From python documentation: class sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep)

my statement: s = sched.scheduler(time.monotonic, time.sleep)

Time sleep should be at a resolution of 100 nanoseconds and also should be a non-issue. https://docs.python.org/3/library/time.html#time.sleep

I have tried changing the priority in case there are other processes to seemingly no effect. https://bugs.python.org/issue13449 https://bugs.python.org/issue44681

Can someone explain what I am missing? or just tell me to use CRON

  • `Please the enter the time which you would like the function to run today` This message does not appear anywhere in the code you posted. It's hard for us to help when you do not show the whole code... – John Gordon May 03 '23 at 00:18
  • No sure but `sched` module maybe pausing the thread. Apparently `time.monotonic()` can be more reliable (I never used it); there goes some info [medium.co](https://medium.com/techtofreedom/5-built-in-timers-in-python-differences-and-usages-596c4c815457); hope it helps. – Memristor May 03 '23 at 00:41

0 Answers0