Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Python use all processors in thread mode?

While developing a Django app deployed on Apache mod_wsgi I found that in case of multithreading (Python threads; mod_wsgi processes=1 threads=8) Python won't use all available processors. With the multiprocessing approach (mod_wsgi processes=8 threads=1) all is fine and I can load my machine at full.

So the question: is this Python behavior normal? I doubt it because using 1 process with few threads is the default mod_wsgi approach.

The system is:

2xIntel Xeon 5XXX series (8 cores (16 with hyperthreading)) on FreeBSD 7.2 AMD64 and Python 2.6.4


Thanks all for answers. We all found that this behavior is normal because of GIL. Here is a good explanation: http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/ or stackoverflow GIL discussion: What is a global interpreter lock (GIL)?.

like image 229
HardQuestions Avatar asked Feb 10 '10 11:02

HardQuestions


People also ask

Do threads run on different cores Python?

Python threads cannot take advantage of many cores. This is due to an internal implementation detail called the GIL (global interpreter lock) in the C implementation of python (cPython) which is almost certainly what you use.

Does Python utilize all cores?

Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.

How many CPUs will the Python threading library use?

How many CPUs (or cores) will the Python threading library take advantage of simultaneously? Python threading is restricted to a single CPU at one time. The multiprocessing library will allow you to run code on different processors. Python threading is restricted to a single CPU at one time.

Does Python multiprocessing use multiple threads?

Multiprocessing is a technique where parallelism in its truest form is achieved. Multiple processes are run across multiple CPU cores, which do not share the resources among them. Each process can have many threads running in its own memory space.


1 Answers

Will Python use all processors in thread mode? No.

Python won't use all available processors; is this Python behavior normal? Yes, it's normal because of the GIL.

For a discussion see http://mail.python.org/pipermail/python-3000/2007-May/007414.html.

You may find that having a couple (or 4) of threads per core/process can still improve performance if there is some blocking, for example waiting for a response from the database would cause that process to block other connections otherwise.

like image 89
John La Rooy Avatar answered Sep 18 '22 12:09

John La Rooy