[mod_python] Re: PyThreadState_Swap(NULL)

Bryan belred at gmail.com
Sat Aug 19 23:21:35 EDT 2006

Graham Dumpleton wrote:
>> 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.
> Whoops, that should be mod_python 3.2.10. Not 3.2.8.
> Graham

hi graham,

we are using mod_python 3.2.10.  you are right that we are doing something 
similar to mod_python in that we fully embed python to process requests using 
our protocol.  the difference is that our main protocol and message dispatcher 
has nothing to do with apache and can plug into our own proprietary server as 
well as 3rd party servers. our mod_xxx module is really just an adapter to let 
our dispatcher plug into apache.  and our python adapter is a plug into our 
dispatcher along with other language adapters.  this is a snippet of our code 
and shows you where the failure is.  the failure occurs in the start function 
and g_py_main_interpreter returned by PyThreadState_Swap(NULL) is NULL.  i 
wouldn't mind grabbing your latest code from svn and looking at your new API, 
except that i'm not sure using experimental code at this phase of development 
process would be wise.  also, if i read what you wrote correctly, i would need 
to tie our code to use the mod_python framework and i'm not willing to do that 
since our python adapter has no knowledge of apache.  unless i'm wrong, i 
believe the only real solution at this point is to use two apache processes. 
one that includes mod_python which we use for cgi gui apps, and the other with 
our dispatcher so there would be no conflicts.  this wouldn't be an optimal 
solution, but it's doable.

this is an example of our python adapter code and http.conf

--- mod_xxx ---

// this is an example of start

if (!Py_IsInitialized()) {

g_py_main_interpreter = PyThreadState_Swap(NULL);
if (g_py_main_interpreter) {
      log("PyThreadState_Swap(NULL) succeeded");
else {
      log("PyThreadState_Swap(NULL) failed");

// this is an example of the main processing

py_interp = Py_NewInterpreter();
... do normal python stuff here including executing python scripts

// this is an example of stop

g_py_main_interpreter = NULL

---- apache's http.conf ----

LoadFile  "c:/Program Files/Common Files/Company/python/python24/python24.dll
LoadModule python_module  "../apache/bin/mod_python.so"
LoadModule mod_xxx        "bin/mod_xxx.dll

Listen 4119
<VirtualHost _default_:4119>
      <Directory "C:/Program Files/company/htdocs/xxx">
          SetHandler    mod_python
          PythonHandler mod_python.publisher
          PythonDebug   Off


More information about the Mod_python mailing list