How are threads implemented in Python?

Aahz Maruch aahz at panix.com
Sat Jan 27 11:40:11 EST 2001


In article <OKCc6.46$wI1.8606 at news1.rdc1.mb.home.com>,
Parzival Herzog <parz at home.com> wrote:
>
>Are threads implemented as OS threads, thereby multi-threading the
>interpreter code, or does the Python interpreter itself maintain a list
>of Python treads and switch between them?
>
>If the interpreter runs in multiple OS threads, then it must acquire
>an release a mutex lock very frequently, so that the run-time data
>structures are operated on in atomic way. Such a lock would a) be
>locked very nearly all of the time any Python thread is active, so
>that the multiple interpreter threads would in fact be operating one
>at a time with frequent opertunities to switch.  In addition, whenever
>code in external libraries manipulates Python objects a mutex must be
>acquired and released.

Standard Python threads use OS-level threads, and the Global Interpreter
Lock is indeed an issue.  Note that external libraries can release the
GIL when not dealing with Python objects, and most of the I/O calls in
Python are already coded that way (thereby providing a heavy performance
boost for threaded I/O).

There's also an add-on patch that allows Python-level threads called
micro-threads.  It's part of a larger patch for continuations.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"I heard Commentary and Dissent had merged and formed Dysentary."



More information about the Python-list mailing list