Dedicated CPU core for Python?

John Nagle nagle at animats.com
Fri Apr 27 15:38:22 EDT 2007


Joshua J. Kugler wrote:
> On Thursday 26 April 2007 14:07, Gabriel Genellina wrote:
> 
> 
>>En Thu, 26 Apr 2007 15:54:38 -0300, Joshua J. Kugler
>><joshua at eeinternet.com> escribió:
>>
>>
>>>Are you talking about CPU affinity
>>>(http://en.wikipedia.org/wiki/Processor_affinity) or an actual CPU that
>>>can directory execute Python byte code?  If the former, CPython only
>>>uses one
>>>CPU core right now because it's threads are all internal, and do not
>>>spawn system threads (IIRC).
>>
>>Python threads are OS threads:
>>http://docs.python.org/lib/module-thread.html
>>"[The thread module] is supported on Windows, Linux, SGI IRIX, Solaris
>>2.x, as well as on systems that have a POSIX thread (a.k.a. ``pthread'')
>>implementation."
> 
> 
> Yes, that may be, but they are not true system threads, or at least do not
> appear to be.  Threads on linux each show up as a separate process.  I can
> have several threads in my Python program, but no additional processes show
> up in ps -A.  I don't see how Python threads can be system threads with the
> GIL.  But, I've been wrong before, and threads are something I have very
> light knowledge of.

     OK.  What's going on inside CPython is this:

     Each Python thread is an operating system thread, managed through
the POSIX pthreads interface.  On a system with more than one CPU,
more than one such thread can run at a time.

     But, because the CPython system isn't really reentrant, most of the
data structures have to be protected against simultaneous access by the Global
Interpreter Lock.  So, when you have multiple compute-bound threads
on multiple CPUs in Python, at any one time, no more than one of them
is executing the CPython interpreter.  The others are blocked on the
Global Interpreter Lock.

     The lock is unlocked before I/O requests and such, which allows another
thread to run while threads are blocked waiting for an I/O operation to
complete.

     That's why having multiple CPUs ("dual core" in marketing jargon)
won't improve Python performance.

     This is an implementation limitation of CPython and its libraries.
Jython and Iron Python don't have this issue.

				John Nagle



More information about the Python-list mailing list