有没有办法获取所有事件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/