Python, Panda and multicores - A Summary
Hello!
Since a few month I own a multicore CPU and with that I realized that pyhton is only running on a single core and not utilizing more than one core. I tried to find out whats wrong, cos my code was threaded.
I noticed that many coders stumble over this problem so it thought it might be helpful to summarize what I found out about Python, the global interpreter lock (GIL) and multi-threading vs. -processing.
I have to admit that I dont have decent knowledge about the ins and outs of this topic, so if you notice mistakes or I was missing something it would be nice to point it out and I will add it to this summary.
The Panda forum offers two threads about multicore utilisation so far. I took two quotes form them (David and Yosh) about Panda and multicores.
discourse.panda3d.org/viewtopic.php … +threading
discourse.panda3d.org/viewtopic.php … =multicore
In the link section you will find the links to all papers I took into account of this summary, so feel free to have a look at them.
thx
> The GIL - global interpreter lock <
The GIL prevents multiple threads to run at the same time, so they cant interfere. But this also means you cant run threads on different CPUs, too. It is important to understand the GIL, cos else you will wonder why your pyhton-code is only running on a single CPU on your shiny new “multi-octo-core-CPU”. GIL is not a part of Python but of the interpreter cPyhton.
The GIL has advantages (no locks, no interferences of threads) and disadvantages (cant utilize multicores via threading).
Currently, the only languages that support multi-core threading are Java and C++, as far as I understand it. Most other languages go other ways.
It is often requested to remove GIL from pyhton or at least make it optional, but up to now no usefull solotion has been found. 1999 it was tried to remove GIL with a fork of pyhton 1.5 (?) with drastical performance reductions when running threaded (30-50%).
Also, there are ways to bypass the GIL. One way are c-extension. From what I read c-extensions release the GIL so multicore threading becomes available. A good read might be the article “Threading the Global Interpreter Lock”.
Related to Panda theres also an interesting quote from Yosh:
and a quote from David:
> Multi-threading vs. multi-processing <
Van Rossum said in this blog that we have to get over the “brainwashing that threads are the only way to do multicoreing”. Cos of the GIL, the current “phythonic way” seems to be multi-processing - running different processes that communicate to each other via IPC (interprocess communication). Multiprocessing has the advantage that you dont need to have your processes on the same machine, so its the common way for clusters (lots of networked computers).
A problem is that python currently isnt offering neat ways to realize easy and painless IPC, YET.
> IPC - Interprocess Communication <
Rather than trying to explain it myself I let the wiki speak. You will also find different ways to utilize IPC with pyhton, but also notice that most of this is (unfortunatly) platform specific.
From the wiki:
source: en.wikipedia.org/wiki/Interprocess_communication
MPI APIs like pyMPI might also provide ways to mulicoreing, but I cant provide much to that topic.
APIs that provide multicore-access:
Jython via Java VM
Iron Python via MS VM(?)
Parallel Python: parallelpython.com/
PyMPI: pympi.sourceforge.net/ (outdated?)
APIs that use sockets, rather than coding on pure sockets (e.g. Twisted network)
> Links <
How GIL works:
Thread State and the Global Interpreter Lock:
docs.python.org/api/threads.html
Threading the Global Interpreter Lock:
pyzine.com/Issue001/Section_ … reter.html
About “remove GIL” requests:
open letter Brendel: blog.snaplogic.org/?p=94
response van Rossum: artima.com/weblogs/viewpost. … ead=214235
Ongoing discussion:
van Rossum blog: artima.com/forums/flat.jsp?f … msRange=15
brendel blog: blog.snaplogic.org/?p=94
Problems of removing GIL: mail.python.org/pipermail/python … 17099.html
Latest multi-core improvements from the core developer A. Olsen (9/2007):
The posts from Olsen are a very interesting read!
artima.com/forums/flat.jsp?f … msRange=15
.
.