Here_2_learn
Here_2_learn

Reputation: 5451

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

Upvotes: 6

Views: 6649

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601351

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

for thread in threading.enumerate():
    print(thread.name)

Upvotes: 7

Related Questions