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?
Thanks for help.
In Python, the method threading. active_co unt() from the threading module is used to count the currently active or running threads.
get_ident() works, or threading. current_thread(). ident (or threading. currentThread().
Creating Thread Using Threading ModuleDefine a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started.
You can use threading.enumerate() : Python documentation about it here
for thread in threading.enumerate(): print(thread.name)
threading.enumerate() can be used for getting the list of running threads (Thread objects). As per library reference, running threads imply
It excludes Threads that are not yet started and already terminated.
You can use threading.active_count to get the length of the list returned by threading.enumerate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With