如何知道线程在运行:python

2024-09-26 22:42:01 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有任何方法可以知道哪些线程正在使用python线程模块运行。通过下面的代码,我可以获得线程名称、当前线程、活动线程计数。在

但我怀疑的是活动线程是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())

以上代码的输出:

主线程

当前线程<;\u MainThread(主线程,开始于11008)>

2个活动线程


Tags: 模块方法代码import名称timecurrent线程

热门问题