Python:同时运行多个计时器 [英] Python: Running multiple timers simultaneously

查看:892
本文介绍了Python:同时运行多个计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个循环中创建多个计时器.当循环终止时,应该有多个计时器在运行.如果任何计时器超时,则应调用另一个函数. 如何在Python中实现呢? 任何帮助将不胜感激.

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.

例如

for i in (0,6):
   do something
   start timer_i

for i in (0,6):
   if timer_i times out:
       call another function

推荐答案

查看Timer,它位于python标准库的threading模块中.该文档提供了以下示例:

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

这篇关于Python:同时运行多个计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆