multithreading Python 正在运行线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37940460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python getting running Threads
提问by Davide Ruzza
I'm doing a project with python and in my code i had to start some threads. Now i need to call a thread to stop it from, but from another class. Is there some way to get a list of all running threads?
我正在用 python 做一个项目,在我的代码中我必须启动一些线程。现在我需要调用一个线程来阻止它,但是来自另一个类。有没有办法获得所有正在运行的线程的列表?
Thanks for help.
感谢帮助。
回答by kaiser
回答by Vineel
threading.enumerate()can be used for getting the list of running threads (Thread objects). As per library reference, running threads imply
threading.enumerate()可用于获取正在运行的线程(Thread 对象)列表。根据库参考,运行线程意味着
- All Thread objects that are currently alive, created using threading module
- Daemonic threads (whose presence doesn't prevent the process from exiting)
- Dummy thread objects created by current thread (Threads directly created from C code. They are always alive and daemonic and cannot be joined)
- Main Thread (Default thread in python)
- 当前活动的所有线程对象,使用线程模块创建
- 守护线程(其存在不会阻止进程退出)
- 当前线程创建的虚拟线程对象(直接从 C 代码创建的线程。它们始终处于活动状态和守护进程,无法加入)
- 主线程(python 中的默认线程)
It excludes Threads that are not yet started and already terminated.
它不包括尚未启动和已终止的线程。
You can use threading.active_countto get the length of the list returned by threading.enumerate
可以使用threading.active_count获取 threading.enumerate 返回的列表长度