wiki:ModPythonIntegration
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 2 (modified by leo mendoza, 9 years ago) (diff)

fixed exception handling bringing down entire server

Integrating mod_python with TurboGears .8a1

As always, this is what worked for me. YMMV. And $TG_MP = [your turbogears project directory].

Go grab the mpcp.py from  http://www.jamwt.com/ and put it into $TG_MP. Ideally, you could just grab it from easy_install, but I had a problem getting the mpcp to be seen in mod_python's $PYTHONPATH so that was my workaround.

Put an .htaccess file in $TG_MP with the following:

SetHandler mod_python
PythonHandler mpcp
PythonDebug On
PythonOption cherrysetup $NAME_OF_YOUR_START_SCRIPT::mp_setup

Warning! The start script is usually MyAppName?-start.py, and the dash will screw up the PythonOption. Ergo, lose the dash and there shouldn't be any problems.

Now, in your (newly renamed) MyAppName_start.py make the following changes:

1) move cherrypy.server.start() so it only starts if name == "main". This might be unnecessary, but I really enjoy typing, so whatever.

if __name__ == "__main__":
   cherrypy.server.start()

2) create a new no-op method, called mp_setup(). As such:

def mp_setup():
   pass

This method is supposed to have cherrypy production config stuff, but our prod.cfg file handles that, so make it a no-op. The better solution would be to remove the line in mpcp that looks for cherrysetup, but that's something I'll worry about when a push to production is near.

Now, in mpcp.py add the following lines, as per Robert's patch.

...
def handler(req):
        config = req.get_config()
        options = req.get_options()
        setup(options)

+       # Create new threadlocal objects for the current request/response
+       cherrypy.request.purge__()
+       cherrypy.response.purge__()

        # Vars needed by cherrypy
        clientAddress = req.connection.remote_ip 
...

Also, in mpcp.py you may want to make the following change. In the current implementation, when an exception occurs it brings down the cherrypy server. But, because its persistent, it never gets brought back up. To fix this:

def setup(options):
           global _isSetup
...            
-          cherrypy.server.start(initOnly=True)
+          try:
+              cherrypy.server.start(initOnly=True)
+          except cherrypy.NotReady:
+              cherrypy.server.restart()
...

I assume at some point, the mpcp will be changed to match the new CherryPy? thread handling routine. The mpcp used for this mini HOWTO was 1.0... double check to see the purge requests aren't there already.

Good luck!