python - 列出所有事件的多线程.Timer 对象

标签 python multithreading timer

有没有办法获取所有事件threading.Timer的列表对象以便 cancel退出前所有这些?否则程序将挂起以等待完成。

我可以保留它们的列表,但当它们触发时我也必须将它们删除。相反,我想知道是否有办法获得所有这些。

最佳答案

threading.enumerate() 返回一个列表,其中包含事件的计时器 - 它们是 threading._Timer 的实例。

>>> t = threading.Timer(30, foo, kwargs = d)
>>> t.start()
>>> t.isAlive()
True
>>> for thing in threading.enumerate():
    if isinstance(thing, threading._Timer):
        thing.cancel()


>>> t.isAlive()
False
>>> 

关于python - 列出所有事件的多线程.Timer 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30919079/

相关文章:

c# - 关于 System.Timers.Timer 的困惑

python - 如何并行处理数据库中的: 6 machines, 800万个独立文档

python-3.x - 一旦主循环启动,PyStray 就会停止工作(分离模式)

java - @Scheduled方法无法使用存储库

c# - 当后台有大量代码时,如何让表单刷新?

iphone - 计时器未正确倒计时

python - 在 Python 3 中加载 Python 2 .npy 文件时出错

python - setup.py build 不起作用,但开发可以

python - 概率网格 matplotlib

javascript - 如果用户不使用浏览器,jQuery 计时器就会停止