Python threads question

Aahz Maruch aahz at netcom.com
Sun Aug 20 15:05:15 EDT 2000


In article <8np7j8$763$1 at news7.svr.pol.co.uk>,
Makhno <mak at imakhno.freeserve.co.uk> wrote:
>
>I'm writing a GUI management tool and in order to distinguish it from other
>such tools I'm trying to take advantage of multithreading.
>I need some confirmations on how Python threads work. From Beazely's book I
>am given the impression that the threads are simulated, ie: the Python
>interpreter runs in a single low-level thread (where I state 'low-level' to
>mean that it was created with a native-OS call) but constantly switches
>between instructions in different python threads (every 10 bytecodes of
>instructions).

Close, but no ceegar.  There are indeed multiple native threads, but
there is a lock that prevents more than one from running Python code at
any time.  That lock is switched to another thread roughly every ten
bytecodes (that number is changeable, though) by the Python interpreter
itself as it operates on bytecodes.

>What this effectively means is that when calling an external function
>(eg: a C-function extension) that all Python threads must immediately
>halt until it returns.  Also, it implies that there is only ever one
>thread running in the python interpreter at a time: That that is
>attached to the python process.

Quite the contrary.  Thread-safe external code can *release* that lock;
as I said in another post just now, all the blocking I/O calls in Python
do this automatically.  The only gotcha here is that if you have an
external function that does *not* release the lock, it doesn't go
through the section of the Python interpreter that switches between
threads, so other Python threads are blocked.  You need to re-acquire
the interpreter lock when you want to communicate with the Python
interpreter.

I'll skip responding to the rest of your post to give you some time to
rethink how you're handling stuff.

Note: I've got a pretty good understanding of the Python threading
model, but I've done no work with extensions, so don't ask me too many
details about how to do the lock acquiring and releasing from the
extension side.

Note^2: One of the side effects of Christian Tismer's Stackless Python
is that it removes the Global Interpreter Lock.  You might want to
investigate that; the only problem is that you'll not be running on
stock Python any more -- primarily an issue if you need to distribute
your application or have an organizational policy against non-stock
patches.
--
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

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

"A foolish consistency is the hobgoblin of little minds, adored by little 
statesmen and philosophers and divines."  -- Ralph Waldo Emerson



More information about the Python-list mailing list