Section 18.3. Running CherryPy on mod_python


18.3. Running CherryPy on mod_python

Another popular deployment option is to use mod_python, which is basically a Python interpreter embedded in each of your web server processes.

18.3.1. Advantages/Drawbacks of Using mod_python

The advantages of this method are as follows:

  • Slightly faster than mod_rewrite (because requests don't need to be forwarded)

  • No need to start/stop your CherryPy process yourself (because it starts with Apache)

The drawbacks of this method are as follows:

  • Apache starts multiple processes, so you can't use RAM-based sessions, for instance.

  • A bit harder to configure than mod_rewrite.

  • mod_python is not yet widespread among web hosting companies. (One current problem is that Apache needs to be restarted whenever you change something in your CherryPy code.)

  • Might be less stable than mod_rewrite.

For some people who are using shared hosting, this might be your only option because your ISP will not allow long running processes.

18.3.2. Configuring Apache/CherryPy for mod_python

The recommended way to run CherryPy with mod_python is to use the mpcp module available from http://jamwt.com/. The following configuration example applies to Apache 2.0, mod_python 3, Python 2.4, and CherryPy 2.2. Here is how you configure Apache:

ServerName 'cherrypy.example.com' PythonPath "['/path/to/project-root'] + sys.path' <Location "/">        SetHandler mod_python        PythonHandler mpcp        PythonOption cherrysetup control.main.root::mp_setup </Location>


And here is sample code showing how to set up CherryPy for mod_python:

import cherrypy class CherryApp:        @cherrypy.expose        def index(self, **kwargs):               return 'Hello, World!' cherrypy.root = CherryApp() if __name__ == '__main__':        # CP server testing with autoreload, etc        cherrypy.config.update({'global' : {'server.socket_port' : 8000}})        cherrypy.server.start() def mp_setup():        """mod_python production setup"""        cherrypy.config.update({'global' : {'server.environment' : 'production'}})





Rapid Web Applications with TurboGears(c) Using Python to Create Ajax-Powered Sites
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
ISBN: 0132433885
EAN: 2147483647
Year: 2006
Pages: 202

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net