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:

    1. <VirtualHost *>
    2. WSGIDaemonProcess my_process
    3. WSGIProcessGroup my_process
    4. </Location>
    5. WSGIDaemonProcess my_other_process
    6. <Location /my_other_app>
    7. WSGIProcessGroup my_other_process
    8. 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.