PyThreadState_Swap(NULL)

Jack Diederich jack at psynchronous.com
Sat Aug 19 10:50:56 EDT 2006


On Fri, Aug 18, 2006 at 02:21:40PM -0700, Bryan wrote:
>
> i've written a program that uses python c api code that lives in a 
> shared library that is loaded by a custom apache module (mod_xxx).  this 
> python c api code all works correctly under our test server and under 
> apache but only if mod_python isn't loaded.  when apache loads 
> mod_python as shown in the http.conf snippet below, 
> PyThreadState_Swap(NULL) in mod_xxx returns NULL.  when the snippet of 
> code in http.conf is commented out, it works again.  what do i have to 
> do to have mod_xxx code work correctly when apache loads mod_python?
> 
> 
> failure case when apache loads mod_python:
>      Py_Initialize() succeeded
>      PyThreadState_Swap(NULL) failed
> 
> 
> sucess case when apache doesn't load mod_python:
>      Py_Initialize() succeeded
>      PyThreadState_Swap(NULL) succeeded
> 

Running multiple python interpreters in the same process isn't supported.
It works OK for most things but some low-level guts are shared between
interpreters so it is possible to run into trouble.

You aren't running multiple interpreters in the same process.  You and
mod_python both think you are in charge and end up nuking each other's 
states. Py_Initialize() resets the global state and shouldn't be called 
more than once.  You can create more than one sub-interpreter (check out
the mod_python source for how, the source is small and readable).

The best thing to do would be to load your module last and conitionally
call Py_Initialize() if someone else hasn't already.

-Jack



More information about the Python-list mailing list