在使用多处理模块的 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:
对于 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 多处理的池进程限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象