如何限制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.
例如,如果我想调用程序的主函数并且仅使用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屋!