Python Thread safety

Warren Postma embed at NOSPAM.geocities.com
Wed Jun 28 10:13:35 EDT 2000


<denalione at my-deja.com> wrote in message news:8jcu30$2nm$1 at nnrp2.deja.com...
> What is the current status of thread safety in Python? I saw references to
> Python not being thread safe but not what version was being discussed.


If you are writing a 100% python application, on a platform where threads
are supported (Windows, or any Unix with appropriate user or kernel-level
threading support, etcetera) then I can't think of any problems with
threading on Python, it works perfectly, except that the granularity of
scheduling is perhaps less than some applications might need.

If you are writing a multithreaded C application , see the excellent article
in Linux Journal (May 2000) on Embedded Multithreaded Python. There is some
special code to add to your C threads to make them interoperate nicely with
Python.

There is a global interpreter lock, meaning that the interpreter can run in
only one thread at a time, however in practice this is hardly a problem.  If
you only have ONE cpu, then only one thread is executing at a time, it's
just that the global interpreter lock prevents the operating system's thread
context switching from surprising Python and "pulling the rug out from under
it's feet".  Python decides when to switch between Interpreter-threads, and
it does this nicely.    I'm impressed. Python's beauty isn't just skin deep
(the syntax, and the experience of using it as a scripting language), the
guts are beautiful too, at least to an experienced C programmer. The C code
that implements it is both a wonder of portability and beautiful
architecture. Some brouhaha has arisen on the newsgroup about the
global-interpreter-lock being a fudge, but I disagree.  I went in and poked
around, figured out the thread context switching, and I think it's just
great.

I am running Python in a deeply embedded board-level industrial computer,
running a massively multithreading application (I estimate 50 to 100
threads). About half those will have python thread state data, and half will
be C-only threads.    So far, it's working great and the Linux Journal
article documented the perfect solution for my problem.


Warren Postma





More information about the Python-list mailing list