sankalpjonn / timeloop

An elegant periodic task executor
MIT License
245 stars 49 forks source link

Execution time of task is delaying next periodic run #9

Open helderdaniel opened 5 years ago

helderdaniel commented 5 years ago

Hi,

When computing the time for the next periodic execution of the task, the execution time of the task itself is not taken into consideration.

The effect is that at each task run, the execution time slot will be delayed by the time that the task took to execute.

Let’s say we start a task with period 10 secs and execution time of about 4 secs:

import time
from timeloop import Timeloop
from datetime import timedelta

tl = Timeloop()

@tl.job(interval=timedelta(seconds=10))
def sample_job_every_10s():
  time.sleep(4)
  print “10s job current time: : {}”.format(time.ctime())

if __name__ == “__main__”:
  tl.start(block=True)

We can see that each run is started 14 secs after the last, not 10:

10s job current time: : Tue Aug 20 10:59:27 2019
10s job current time: : Tue Aug 20 10:59:41 2019
10s job current time: : Tue Aug 20 10:59:55 2019
10s job current time: : Tue Aug 20 11:00:09 2019
(…)

To make an accurate scheduler, that starts the task with the period specified, it is needed to subtract the execution time of the task to the next period, thus having execution times for each run with the period specified:

Tue Aug 20 11:29:38 2019
Tue Aug 20 11:29:48 2019
Tue Aug 20 11:29:58 2019
Tue Aug 20 11:30:08 2019
(…)
ukchukx commented 5 years ago

Fixed this in my fork and created a pull request.

debackel commented 4 years ago

Fixed this in my fork and created a pull request.

Hi, I have tested the original which indeed drifts. And then your fork, with precisely the same code, but for whatever reason the recurring event doesn't seem triggered at all. Did someone else experienced the same issue?