I want to perform an action at a regular interval in my multi-threaded Python application. I have seen two different ways of doing it
exit = False def thread_func(): while not exit: action() time.sleep(DELAY)
exit_flag = threading.Event() def thread_func(): while not exit_flag.wait(timeout=DELAY): action()
Is there an advantage to one way over the other? Does one use less resources, or play nicer with other threads and the GIL? Which one makes the remaining threads in my app more responsive?
(Assume some external event sets
exit_flag, and I am willing to wait the full delay while shutting down)