我需要检查特定线程是否在threading.enumerate()
的结果内。
假设我得到带有枚举的结果:
<_MainThread(MainThread, started 8568)>
<Thread(Thread-263, started 11116)>
<MyThread(Thread-235, started 21045)>
如何检查结果中是否包含线程
MyThread
? 最佳答案
过滤线程类:
>>> import threading
>>> class MyThread(threading.Thread): pass
...
>>> def do_nothing():
... while True:
... pass
>>> MyThread(target=do_nothing).start()
>>> threading.Thread(target=do_nothing).start()
>>> threading.enumerate()
[<MyThread(Thread-1, started 8040)>, <Thread(Thread-2, started 7352)>, <_MainThread(MainThread, started 2772)>]
>>> [t for t in threading.enumerate() if isinstance(t, MyThread)]
[<MyThread(Thread-1, started 8040)>]
关于python - 在threading.enumerate()中是否有特定线程?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19930615/