有没有办法使用Python threading模块知道哪些线程正在运行。使用以下代码,我能够获取线程名称、当前线程和活动线程数。
但是我的疑问在于活动线程是2,而当前线程总是“MainThread”。那么在后台运行的其他线程可能是什么?
但是我的疑问在于活动线程是2,而当前线程总是“MainThread”。那么在后台运行的其他线程可能是什么?
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())
上述代码的输出:
主线程
当前线程 <_MainThread(MainThread, started 11008)>
活动线程数 2