首页 > 解决方案 > Running Python function at defined intervals

问题描述

I've got a Python loop that should run every minute, do some data processing and sleep until the next minute is up. However the processing takes variable amount of time, sometimes it's close to zero when there is not much to do, sometimes it takes even 10 or 20 seconds.

To compensate for that I measure the time it takes to run the processing like this:

while True:
  time_start = time.time()
  do_something()                        # <-- This takes unknown time
  time_spent = time.time() - time_start
  time.sleep(60 - time_spent)

It kind of works but over a couple of days it still drifts away by a number of seconds. I guess it happens when the computer (small Raspberry Pi) is busy and delays the start of the loop, then it all starts slipping away.

I don't need do_something() executed exactly every minute, so no need for real-time OS or anything like that, but I don't want one delayed start affect all the subsequent ones either.

Is there some kind of scheduler that can run my function at a predefined rate? Or some more clever way to compensate for the occasional delays?

标签: pythonscheduled-tasks

解决方案


Playing with the loop a little this seems to work quite well. The trick is to record the start time once before the loop starts, not on every iteration. That way one delayed start won't affect any future ones.

rate_sec = 60
time_start = time.time()

while True:
    print("{}".format(datetime.now()))

    # Emulate processing time 0s to 20s
    time.sleep(random.randint(0, 20))

    # Sleep until the next 'rate_sec' multiple
    delay = rate_sec - (time.time() - time_start) % rate_sec
    time.sleep(delay)

推荐阅读