PyMongo and mod_wsgi
- Run in daemon mode with the
WSGIDaemonProcess
directive. - Assign each application to a separate daemon with
WSGIProcessGroup
. - Use
WSGIApplicationGroup %{GLOBAL}
to ensure your application is running in the daemon’s main Python interpreter, not a sub interpreter.
For example, this mod_wsgi
configuration ensures an application runs in the main interpreter:
<VirtualHost *>
WSGIDaemonProcess my_process
WSGIProcessGroup my_process
</Location>
WSGIDaemonProcess my_other_process
<Location /my_other_app>
WSGIProcessGroup my_other_process
WSGIApplicationGroup %{GLOBAL}
Background: mod_wsgi
can run in “embedded” mode when only WSGIScriptAlias is set, or “daemon” mode with WSGIDaemonProcess. In daemon mode, mod_wsgi
can 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 main interpreter.
Beginning with PyMongo 2.7, the C extension for BSON detects when it is running in a sub interpreter and activates a workaround, which adds a small cost to BSON decoding. To avoid this cost, use WSGIApplicationGroup %{GLOBAL}
to ensure your application runs in the main interpreter.