我需要检查特定线程是否在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/