观察 Python 中的线程数

Watch number of Threads in Python

我用 Python 写了一个程序,它使用多线程,我想知道有多少线程在计时和所有统计信息下执行,有没有办法使用调试器来获得这些结果?

PS:我正在使用 PyCharm。

假设您正在使用 threading 模块而不是裸露的 threads,您可以按如下方式使用 threading.active_count()

num_threads = threading.active_count() # Python 3.x
num_threads = threading.activeCount() # Python 2.x

获取线程数量。要获取线程本身,请使用 threading.enumerate():

for th in threading.enumerate():
  # do whatever.

就时间和其他统计数据而言,您可能需要手动跟踪这些,看起来 Thread 对象的元数据相当少。