Python getting running Threads Python getting running Threads multithreading multithreading

Python getting running Threads


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

  1. All Thread objects that are currently alive, created using threading module
  2. Daemonic threads (whose presence doesn't prevent the process from exiting)
  3. Dummy thread objects created by current thread (Threads directly created from C code. They are always alive and daemonic and cannot be joined)
  4. Main Thread (Default thread in python)

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