[mod_python] PyThreadState_Swap(NULL)

Graham Dumpleton grahamd at dscpl.com.au
Sat Aug 19 18:05:23 EDT 2006

On 20/08/2006, at 3:02 AM, 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?
> 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.
> any help to get our custom module co-existing with mod_python would  
> be very appreciated.

For a start, which version of mod_python are you using? There were some
important changes in mod_python 3.2.8 to allow third party modules for
Python to work properly where those third party modules used the simple
GIL API for thread management. See:


Whether this would help is debatable though, as by the sound of it you
are doing a full embedding of Python in your own library much like  
does and that may still conflict.

It is possible that you might have to wait until a new API is added  
to mod_python
as discussed in:


The intent with this API was to allow other Apache modules to access the
framework used by mod_python for the construction and access to Python
interpreter instances and to a wrapper for request object etc. That  
way a
third party module doesn't need to do it and no risk of there being a  
in the way each managed it.

This new API hasn't been added yet as no one had stepped up to say they
were interested enough to want to use it. If you are prepared and  
able to use
latest development builds of mod_python from subversion repository, I  
add the minimal functions required and you could start experimenting.

Other than that, can only suggest you post up the most minimal skeleton
possible showing what you are doing, preferably something that could be
compiled and tested.


More information about the Mod_python mailing list