Python: Running multiple timers simultaneously

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:50:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!