PyMongo and mod_wsgi

To run your application under mod_wsgi,follow these guidelines:

  • Run mod_wsgi in daemon mode with the WSGIDaemonProcess directive.
  • Assign each application to a separate daemon with WSGIProcessGroup.
  • Use WSGIApplicationGroup %{GLOBAL} to ensure your application is runningin the daemon’s main Python interpreter, not a sub interpreter.

For example, this mod_wsgi configuration ensures an application runs in themain interpreter:

  1. <VirtualHost *>
  2. WSGIDaemonProcess my_process
  3. WSGIScriptAlias /my_app /path/to/app.wsgi
  4. WSGIProcessGroup my_process
  5. WSGIApplicationGroup %{GLOBAL}
  6. </VirtualHost>

If you have multiple applications that use PyMongo, put each in a separatedaemon, still in the global application group:

  1. <VirtualHost *>
  2. WSGIDaemonProcess my_process
  3. WSGIScriptAlias /my_app /path/to/app.wsgi
  4. <Location /my_app>
  5. WSGIProcessGroup my_process
  6. </Location>
  7.  
  8. WSGIDaemonProcess my_other_process
  9. WSGIScriptAlias /my_other_app /path/to/other_app.wsgi
  10. <Location /my_other_app>
  11. WSGIProcessGroup my_other_process
  12. </Location>
  13.  
  14. WSGIApplicationGroup %{GLOBAL}
  15. </VirtualHost>

Background: mod_wsgi can run in “embedded” mode when only WSGIScriptAliasis set, or “daemon” mode with WSGIDaemonProcess. In daemon mode, mod_wsgican run your application in the Python main interpreter, or in sub interpreters.The correct way to run a PyMongo application is in daemon mode, using the maininterpreter.

Python C extensions in general have issues running in multiplePython sub interpreters. These difficulties are explained in the documentation forPy_NewInterpreterand in the Multiple Python Sub Interpreterssection of the mod_wsgi documentation.

Beginning with PyMongo 2.7, the C extension for BSON detects when it is runningin a sub interpreter and activates a workaround, which adds a small cost toBSON decoding. To avoid this cost, use WSGIApplicationGroup %{GLOBAL} toensure your application runs in the main interpreter.

Since your program runs in the main interpreter it should not share itsprocess with any other applications, lest they interfere with each other’sstate. Each application should have its own daemon process, as shown in theexample above.