Python: Running multiple timers simultaneously

2019-08-06 14:16发布

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条回答
贪生不怕死
2楼-- · 2019-08-06 14:48

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
查看更多
登录 后发表回答