Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know what are the threads running : python

Is there any way to know what threads are running using python threading module. With the following piece of code, I am able to get the Thread name, current thread, active thread count.

But my doubt here is ACTIVE_THREADS are 2 and the CURRENT THREAD is always "MainThread". What could be the other thread which is running at the back ground ?

import threading
import time

for _ in range(10):
    time.sleep(3)
    print("\n", threading.currentThread().getName())
    print("current thread", threading.current_thread())
    print("active threads ", threading.active_count())

output of the above the code :

MainThread

current thread <_MainThread(MainThread, started 11008)>

active threads 2

like image 230
Here_2_learn Avatar asked Jul 26 '16 13:07

Here_2_learn


1 Answers

You can access all current thread objects using threading.enumerate(), e.g.

for thread in threading.enumerate():
    print(thread.name)
like image 176
Sven Marnach Avatar answered Nov 04 '22 03:11

Sven Marnach