Forum Archive

Best way to implement recurring task

gendalus

Hi there,

I'm currently working on a project what should call an API about every minute (the exact timing is not that important). In order to do it I have a while loop wich is paused after each loop for 60 seconds.

def loop():
    while True:
        update()
        time.sleep(60)

loop()

Is there any better way to do this? It seems, that when I'm running the script and the script is actually paused you can't stop the script with tapping on the [x]. It seems to be waiting for the sleep to end and is terminating after that.

omz

Right now, sleep can't be interrupted. If you want to sleep for such a long time, I would recommend calling it multiple times instead:

for i in xrange(60):
  time.sleep(1)
JonB

If this is from a ui, you could use ui.delay at the end of an function, which calls itself. Cancel would be handled by ui.cancel_delays(), either by button, or from will_close, or by checking on_screen etc.