Python 多线程 max_workers

标签 python multithreading cpu

根据documentation ThreadPoolExecutor

If max_workers is None or not given, it will default to the number of processors on the machine.

如果我不给它设置这样的值

ThreadPoolExecutor(max_workers=None)

如果我的值很低 (2) 会不会影响性能? python 是否已经为 None 值分配了所有 CPU 进程,还是仅为带有数字的值分配了 2 个进程?

最佳答案

首先,您似乎在链接中引用了文档的错误部分,即针对进程而非线程的部分。 one for concurrent.futures.ThreadPoolExecutor状态:

Changed in version 3.5: If max_workers is None or not given, it will default to the number of processors on the machine, multiplied by 5, assuming that ThreadPoolExecutor is often used to overlap I/O instead of CPU work and the number of workers should be higher than the number of workers for ProcessPoolExecutor.


由于您使用的是线程,而不是进程,因此假设您的应用程序是 IO 绑定(bind)的,而不是 CPU 绑定(bind)的,并且您将其用于 concurrency, not parallelism .您使用的线程越多,您获得的并发性就越高(在一定程度上),但您获得的 CPU 周期就越少(因为会有上下文切换)。您必须在典型的工作负载下测试您的应用程序,以了解最适合您的方法。对此没有普遍最优的解决方案。

关于Python 多线程 max_workers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40492894/

相关文章:

operating-system - 关于基于 ROM 的可执行文件的基本问题

python - 似乎scikit-learn没有正确构建

python - 正则表达式分割字符串而不考虑引号中的空格

Python 库/python2.7/lib-dynload/_io.so : undefined symbol: _PyErr_ReplaceException

c++ - 为什么这个 C++ 程序没有使用 100% CPU 而只使用 10%

c++ - 如何使用GetNumaProximityNode (Win7+)?

python - 使用 matplotlib 在 python 中绘制曲线决策边界

c++ - 如何优化同时多线程的代码?

java - 安排常规任务而不扩展TimerTask?

python - 信号处理程序在线程启动后不起作用