Thread safety of the python API

Tim Peters tim.one at comcast.net
Thu Mar 13 20:49:58 EST 2003


[Stephen Ludin]
> I am developing an application that I may want to make scriptable with
> python.  If I do so I'd like to use the convenience of the Python API
> in the rest of my application for containers and user defined method
> dispatch.  I am concerned though about using the API in a
> multit-hreaded program.

As you should be.  As a not-too-close reading of the Python/C API manual
will reveal, almost all Python API functions require that the caller hold
the global interpreter lock:

    http://www.python.org/doc/current/api/threads.html

The only exceptions are a handful of initialization and finalization
functions, explicitly identified on that page.  Even the simplest API
operations can cause disaster if the lock isn't held -- you don't have to
search for subtleties, one unprotected Py_INCREF or Py_DECREF is enough to
hose you.

Other than that, it's very robust <wink>.






More information about the Python-list mailing list