Python multiprocessing#39;s Pool process limit(Python 多处理的池进程限制)
问题描述
在使用多处理模块的 Pool 对象时,进程数是否受 CPU 核数的限制?例如.如果我有 4 个核心,即使我创建一个包含 8 个进程的池,一次也只能运行 4 个?
In using the Pool object from the multiprocessing module, is the number of processes limited by the number of CPU cores? E.g. if I have 4 cores, even if I create a Pool with 8 processes, only 4 will be running at one time?
推荐答案
你可以要求尽可能多的进程.任何可能存在的限制都将由您的操作系统施加,而不是由 multiprocessing
施加.例如,
You can ask for as many processes as you like. Any limit that may exist will be imposed by your operating system, not by multiprocessing
. For example,
p = multiprocessing.Pool(1000000)
在任何机器上都可能遭受丑陋的死亡.当我输入这个时,我正在我的盒子上尝试它,并且操作系统正在将我的磁盘磨成灰尘,疯狂地换出 RAM - 最后在它创建了大约 3000 个进程后将其杀死;-)
is likely to suffer an ugly death on any machine. I'm trying it on my box as I type this, and the OS is grinding my disk to dust swapping out RAM madly - finally killed it after it had created about 3000 processes ;-)
至于一次"运行多少个,Python 没有发言权.这取决于:
As to how many will run "at one time", Python has no say in that. It depends on:
- 您有多少硬件能够同时运行;并且,
- 您的操作系统如何决定将硬件资源分配给您计算机上当前正在运行的所有进程.
- How many your hardware is capable of running simultaneously; and,
- How your operating system decides to give hardware resources to all the processes on your machine currently running.
对于 CPU 密集型任务,创建比运行它们的内核更多的 Pool
进程没有意义.如果您也尝试将您的机器用于其他事情,那么您应该创建比内核更少的进程.
For CPU-bound tasks, it doesn't make sense to create more Pool
processes than you have cores to run them on. If you're trying to use your machine for other things too, then you should create fewer processes than cores.
对于 I/O 密集型任务,可能创建比核心更多的 Pool
进程,因为这些进程可能会花费大部分时间阻塞(等待 I/O 完成).
For I/O-bound tasks, it may make sense to create a quite a few more Pool
processes than cores, since the processes will probably spend most their time blocked (waiting for I/O to complete).
这篇关于Python 多处理的池进程限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 多处理的池进程限制
基础教程推荐
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01