How to limit the number of processors that Python uses
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.
For instance, if开发者_JAVA技巧 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:
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))
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.
You are somehow creating a lot of processes. This can be done by the
os.system()
, os.popen*()
, os.spawn*()
or methods in the popen2
, subprocess
and multiprocessing
libraries.
If you aren't using CPython but another Python implementation such as Jython or Stackless, you can also use up many cores by threading.
You'll have to figure out what you are doing that is causing this, and then make sure you don't do more than 7 of those at once.
I'm not quite sure about your situation, but one thing which might work for you, is to find out number of cores you have ( in python you can use multiprocessing library ), and then limit the number of threads/processes that your program creates to less than one of this number .
精彩评论