How to terminate the function that runs every n seconds

Frank Millman frank at
Thu Jan 15 07:56:10 CET 2015

"Dennis Lee Bieber" <wlfraed at> wrote in message 
news:1j1ebalmgkuskq0ltnv5m4sbm6d3p5f840 at
> Pseudo (Python 2.x) code
> def aThread(delay=600.0): #default 10 minutes
>     while keepRunning:
>         print "thread triggered at %s" % time.time()
>         time.sleep(delay)
>     print "thread was commanded to exit"

I read a variation on this theme some years ago that uses 
event.wait(timeout=delay) to manage the delay.

>From the docs, "block until another thread calls set() to set the flag to 
true, or until the optional timeout occurs."

The benefit is that setting the flag terminates the loop straight away, as 
opposed to waiting for the sleep to finish.

I can show some sample code if anyone is interested.

Frank Millman

More information about the Python-list mailing list