问题
I want to create multiple timers in a loop. When the loop terminates, there should be multiple timers running. If any of the timers times out, it should call another function. How do I implement this in Python? Any help will be appreciated.
eg.
for i in (0,6):
do something
start timer_i
for i in (0,6):
if timer_i times out:
call another function
回答1:
Look into Timer
, which is locate in the threading
module of the python standard library. The documentation gives the following example:
from threading import Timer
def hello():
print("hello, world")
t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
来源:https://stackoverflow.com/questions/26446110/python-running-multiple-timers-simultaneously