如何限制 Python 使用的处理器数量 [英] How to limit the number of processors that Python uses
问题描述
我有一个相当消耗内存的 Python 程序,可以在具有 8 个 CPU 的计算机上运行(使用 Python 3.1 64 位).问题是它使用了所有 8 个处理器以 100% 的使用率,因此冻结了机器并使事情变慢.我如何让 Python 只使用 7 个处理器来释放更多 CPU?我已经在多处理库中多次尝试了 Pool 类,但没有运气.
I have a fairly memory expensive Python program to run on a computer that has 8 CPUs (using Python 3.1 64 bit). The problem is that it uses all 8 processors to 100% usage and thus freezes the machine and makes things slow. How would I make Python use only 7 of the processors to free up more CPU? I have experimented with the Pool class within the multiprocessing library many times, but no luck.
例如,如果我想调用我的程序的 main 函数并让它只使用 7 个处理器,我似乎只需要做以下事情,但没有运气:
For instance, if I want to call the main function of my program and have it use only 7 processors, it seems like all I would have to do is the following, but no luck:
来自多处理导入池池(7, main())
from multiprocessing import Pool Pool(7, main())
谁能告诉我哪里出错了?谢谢!
Can anybody tell me where I am going wrong? Thanks!
推荐答案
我遇到了同样的问题,我通过为池分配少一个来解决这个问题:
I had the same problem, which I fixed by allocating one less to the pool:
pool= multiprocessing.Pool(processes=(multiprocessing.cpu_count() - 1))
通过这样做并让您的池仅使用 CPU-1,您可以为您的 PC 节省一些额外的电量.这对我的 Windows 机器有很大帮助,所以我仍然可以点击进入其他应用程序(W7 64 位).在 Linux 上,无论哪种方式,机器都可以.
By doing this and letting your pool only use CPU-1, you can save some extra power for your PC. This helped tremendously on my windows machine so I could still click around into other applications (W7 64 bit). On Linux, the machine was OK either way.
这篇关于如何限制 Python 使用的处理器数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!