python - 减少 ThreadPoolExecutor 创建的事件线程数?

标签 python multithreading concurrent.futures

对于 ThreadPoolExecutor,我想监控实际运行的线程数。但这似乎是不可能的。事件线程的数量不断增加。对于普通线程,如果目标函数返回,线程将关闭。

import threading, concurrent.futures

def test():
    pass

p_ac=threading.active_count()
#Number of threads=2

#for threads
trs=[None]*10
for i in range(10):
    trs[i]=threading.Thread(target=test)
    trs[i].start

print(threading.active_count()==p_ac)
#True

#for executor
executor=concurrent.futures.ThreadPoolExecutor(10)
for _ in range(10):
    executor.submit(test)
print(threading.active_count())
#Number of threads=12

最佳答案

ThreadPoolExecutor 不会在事件时关闭其线程。 ThreadPoolExecutor(X) 创建 X 个线程并使它们保持事件状态,直到您通过 executor.shutdown() 关闭执行器或执行器本身决定它不关闭需要这么多线程。无论如何,如果执行程序处于事件状态,X 是预期的线程数。

关于python - 减少 ThreadPoolExecutor 创建的事件线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46158755/

相关文章:

python - 防止多处理库中的文件句柄继承

python - 计算文件中数字字符串的总和,而数字位于随机行中

Python:将参数传递给 threading.Thread 实例的正确方法是什么

c++ - GDB 在非线程应用程序中显示 1 个线程

c++ - 调用 std::future::get() 后线程真的启动了吗?

Python ThreadPoolExecutor - 是否保证回调与提交的函数在同一线程中运行?

python - 使用 Django 时如何在命令行中退出 dbshel​​l (SQLite 3)?

python - 仅在内部使用派生类型的 F2PY 包装过程

java - 如何同步 2 个作业/进程

python - 并发 future 以非阻塞方式将任务提交到进程池