問題描述
在使用多處理模塊的 Pool 對象時,進程數(shù)是否受 CPU 核數(shù)的限制?例如.如果我有 4 個核心,即使我創(chuàng)建一個包含 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?
推薦答案
你可以要求盡可能多的進程.任何可能存在的限制都將由您的操作系統(tǒng)施加,而不是由 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)
在任何機器上都可能遭受丑陋的死亡.當我輸入這個時,我正在我的盒子上嘗試它,并且操作系統(tǒng)正在將我的磁盤磨成灰塵,瘋狂地換出 RAM - 最后在它創(chuàng)建了大約 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 沒有發(fā)言權(quán).這取決于:
As to how many will run "at one time", Python has no say in that. It depends on:
- 您有多少硬件能夠同時運行;并且,
- 您的操作系統(tǒng)如何決定將硬件資源分配給您計算機上當前正在運行的所有進程.
- 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 密集型任務(wù),創(chuàng)建比運行它們的內(nèi)核更多的 Pool
進程沒有意義.如果您也嘗試將您的機器用于其他事情,那么您應(yīng)該創(chuàng)建比內(nèi)核更少的進程.
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 密集型任務(wù),可能創(chuàng)建比核心更多的 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).
這篇關(guān)于Python 多處理的池進程限制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!