|
| 1 | +.. _pymongo-and-mod_wsgi: |
| 2 | + |
| 3 | +PyMongo and mod_wsgi |
| 4 | +==================== |
| 5 | + |
| 6 | +If you run your application under |
| 7 | +`mod_wsgi <http://code.google.com/p/modwsgi/>`_ and you use PyMongo with its C |
| 8 | +extensions enabled, follow these guidelines for best performance: |
| 9 | + |
| 10 | +* Run ``mod_wsgi`` in daemon mode with the ``WSGIDaemon`` directive. |
| 11 | +* Assign each application to a separate daemon with ``WSGIProcessGroup``. |
| 12 | +* Use ``WSGIApplicationGroup %{GLOBAL}`` to ensure your application is running |
| 13 | + in the daemon's main Python interpreter, not a sub interpreter. |
| 14 | + |
| 15 | +For example, this ``mod_wsgi`` configuration ensures an application runs in the |
| 16 | +main interpreter:: |
| 17 | + |
| 18 | + <VirtualHost *> |
| 19 | + WSGIDaemonProcess my_process |
| 20 | + WSGIScriptAlias /my_app /path/to/app.wsgi |
| 21 | + WSGIProcessGroup my_process |
| 22 | + WSGIApplicationGroup %{GLOBAL} |
| 23 | + </VirtualHost> |
| 24 | + |
| 25 | +If you have multiple applications that use PyMongo, put each in a separate |
| 26 | +daemon, still in the global application group:: |
| 27 | + |
| 28 | + <VirtualHost *> |
| 29 | + WSGIDaemonProcess my_process |
| 30 | + WSGIScriptAlias /my_app /path/to/app.wsgi |
| 31 | + <Location /my_app> |
| 32 | + WSGIProcessGroup my_process |
| 33 | + </Location> |
| 34 | + |
| 35 | + WSGIDaemonProcess my_other_process |
| 36 | + WSGIScriptAlias /my_other_app /path/to/other_app.wsgi |
| 37 | + <Location /my_other_app> |
| 38 | + WSGIProcessGroup my_other_process |
| 39 | + </Location> |
| 40 | + |
| 41 | + WSGIApplicationGroup %{GLOBAL} |
| 42 | + </VirtualHost> |
| 43 | + |
| 44 | +Background: Python C extensions in general have issues running in multiple |
| 45 | +Python sub interpreters. These difficulties are explained in the documentation for |
| 46 | +`Py_NewInterpreter <http://docs.python.org/2/c-api/init.html#Py_NewInterpreter>`_ |
| 47 | +and in the `Multiple Python Sub Interpreters |
| 48 | +<https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Multiple_Python_Sub_Interpreters>`_ |
| 49 | +section of the ``mod_wsgi`` documentation. |
| 50 | + |
| 51 | +Beginning with PyMongo 2.7, the C extension for BSON detects when it is running |
| 52 | +in a sub interpreter and activates a workaround, which adds a small cost to |
| 53 | +BSON decoding. To avoid this cost, use ``WSGIApplicationGroup %{GLOBAL}`` to |
| 54 | +ensure your application runs in the main interpreter. |
| 55 | + |
| 56 | +Since your program runs in the main interpreter it should not share its |
| 57 | +process with any other applications, lest they interfere with each other's |
| 58 | +state. Each application should have its own daemon process, as shown in the |
| 59 | +example above. |
0 commit comments