python - threading.enumerate() 内是否有特定线程?

标签 python python-multithreading

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

相关文章:

python 找不到 pygame.locals

python - Flask 中的代码 503 与嵌入式 Bokeh 服务器应用程序通过 requests.get() 获取 json 化数据

python - 如何在 Python 中循环一系列函数?

python - 如何在Python中同时启动线程

python - 使用 Python 获取 SharePoint 列表

python - 如何给定时器线程一个名字?

python - 导入一个未打包模块的python类,并在它自己的线程中运行

Python线程 - 内部缓冲区错误 - 内存不足

python - Python 中的线程/多处理

python - 我无法将我在 Seaborn 中的线图的 xticks 设置为相应小时的值