2

我需要检查特定线程是否在threading.enumerate().

假设我通过枚举得到结果:

<_MainThread(MainThread, started 8568)>
<Thread(Thread-263, started 11116)>
<MyThread(Thread-235, started 21045)>

如何检查线程MyThread是否在我的结果中?

4

1 回答 1

2

过滤线程类:

>>> 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)>]
于 2013-11-12T13:42:42.670 回答