Skip to content

Commit 30882a5

Browse files
committed
Improved mod_wsgi docs. PYTHON-567
1 parent 5b1e852 commit 30882a5

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

doc/examples/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ MongoDB, you can start it like so:
2323
gevent
2424
gridfs
2525
high_availability
26-
requests
26+
mod_wsgi
27+
requests

doc/examples/mod_wsgi.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.

doc/faq.rst

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -260,30 +260,7 @@ MongoDB backends for Django sessions and authentication (bypassing
260260

261261
Does PyMongo work with **mod_wsgi**?
262262
------------------------------------
263-
`mod_wsgi <http://code.google.com/p/modwsgi/>`_ is a popular Apache
264-
module used for hosting Python applications conforming to the `wsgi
265-
<http://www.wsgi.org/>`_ specification. There is a potential issue
266-
when deploying PyMongo applications with mod_wsgi involving PyMongo's
267-
C extension and mod_wsgi's multiple sub interpreters.
268-
269-
One tricky issue that we've seen when deploying PyMongo applications
270-
with mod_wsgi is documented `here
271-
<http://code.google.com/p/modwsgi/wiki/ApplicationIssues>`_, in the
272-
**Multiple Python Sub Interpreters** section. When running PyMongo
273-
with the C extension enabled it is possible to see strange failures
274-
when encoding due to the way mod_wsgi handles module reloading with
275-
multiple sub interpreters. There are several possible ways to work
276-
around this issue:
277-
278-
1. Run mod_wsgi in daemon mode with each WSGI application assigned to its
279-
own daemon process.
280-
281-
2. Force all WSGI applications to run in the same application group.
282-
283-
3. Install PyMongo :ref:`without the C extension <install-no-c>` (this will
284-
carry a performance penalty, but is the most immediate solution to this
285-
problem).
286-
263+
Yes. See the configuration guide for :ref:`pymongo-and-mod_wsgi`.
287264

288265
How can I use something like Python's :mod:`json` module to encode my documents to JSON?
289266
----------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)