Why does my Python program average only 33% CPU per process? How can I make Python use all available CPU?

32,897

Solution 1

It appears that you have a 3-core CPU. If you want to use more than one CPU core in native Python code, you have to spawn multiple processes. (Two or more Python threads cannot run concurrently on different CPUs)

As R. Pate said, Python's multiprocessing module is one way. However, I would suggest looking at Parallel Python instead. It takes care of distributing tasks and message-passing. You can even run tasks on many separate computers with little change to your code.

Using it is quite simple:

import pp

def parallel_function(arg):
    return arg

job_server = pp.Server() 

# Define your jobs
job1 = job_server.submit(parallel_function, ("foo",))
job2 = job_server.submit(parallel_function, ("bar",))

# Compute and retrieve answers for the jobs.
print job1()
print job2()

Solution 2

Global Interpreter Lock

The reasons of employing such a lock include:

* increased speed of single-threaded programs (no necessity to acquire or release locks
  on all data structures separately)
* easy integration of C libraries that usually are not thread-safe.

Applications written in languages with a GIL have to use separate processes (i.e. interpreters) to achieve full concurrency, as each interpreter has its own GIL.

Solution 3

From CPU usage it looks like you're still running on a single core. Try running a trivial calculation with 3 or more threads with same threading code and see if it utilizes all cores. If it doesn't, something might be wrong with your threading code.

Share:
32,897
Anh Pham
Author by

Anh Pham

Updated on January 12, 2022

Comments

  • Anh Pham
    Anh Pham over 2 years

    I use Python 2.5.4. My computer: CPU AMD Phenom X3 720BE, Mainboard 780G, 4GB RAM, Windows 7 32 bit.

    I use Python threading but can not make every python.exe process consume 100% CPU. Why are they using only about 33-34% on average?.

    I wish to direct all available computer resources toward these large calculations so as to complete them as quickly as possible.

    EDIT: Thanks everybody. Now I'm using Parallel Python and everything works well. My CPU now always at 100%. Thanks all!