How are threads implemented in Python?

Parzival Herzog parz at home.com
Sat Jan 27 11:32:46 EST 2001


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.

I would imagine that with OS locks being expensive,
the frequent locking-unlocking would result in a lot of
overhead.

If on the other hand the Python interpreter manages
Python threads itself, then no locking needs to be done
in the interpreter code, but what about calls to
functions in external libraries which might block,
thereby halting all other Python threads?

Can anyone provide more infomation on how Python
threads actually operate?

- Parzival








More information about the Python-list mailing list