PyThreadState_Swap(NULL)

Jack Diederich jack at psynchronous.com
Sat Aug 19 12:43:33 EDT 2006


On Sat, Aug 19, 2006 at 09:08:21AM -0700, Bryan wrote:
> Jack Diederich wrote:
> > 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
> 
> 
> thanks for replying jack. i tested what you suggested.
> 
> i put our module after mod_python, commented out Py_Initialize(), and 
> Py_IsInitialized() returned True, but PyThreadState_Swap() failed.
> 
> i put our module before mod_python, called Py_Initialize(), and 
> Py_IsInitialized() returned True, but PyThreadState_Swap() failed.
> 
> i removed mod_python and our module succeeded.
> 
> i even tested combinations of calling and PyEval_InitThreads() but didn't work 
> either.

I'm out of my depth here, I only know that this kind of thing is tricky.
I think you need to explicitly spawn a new sub-interpreter or at least a new
thread state from the existing interpreter.   Your best bet is to ask on the
mod_python mailing list.  Those guys actually do this kind of thing so if you
are going to get good advice it will be from them.

-Jack



More information about the Python-list mailing list