@@ -17,32 +17,35 @@ Gevent's monkey-patching replaces those standard functions so that PyMongo
1717does asynchronous I/O with non-blocking sockets, and schedules operations
1818on greenlets instead of threads.
1919
20- Consideration on gevent's hub termination
21- -----------------------------------------
22- PyMongo uses threads to monitor your servers' topology and to handle
23- graceful reconnections.
20+ Avoid blocking in Hub.join
21+ --------------------------
2422
25- When shutting down your application gracefully, your code be may waiting
26- for any remaining greenlet to terminate before exiting.
23+ By default, PyMongo uses threads to discover and monitor your servers' topology
24+ (see :ref: `health-monitoring `). If you execute ``monkey.patch_all() `` when
25+ your application first begins, PyMongo automatically uses greenlets instead
26+ of threads.
2727
28- This may become a **blocking operation ** because monkey-patched threads
29- are considered as greenlets and this could prevent your application
30- to exit properly. You therefore **must close or dereference ** any active
31- MongoClient object before exiting !
28+ When shutting down, if your application calls :meth: `~gevent.hub.Hub.join ` on
29+ Gevent's :class: `~gevent.hub.Hub ` without first terminating these background
30+ greenlets, the call to :meth: `~gevent.hub.Hub.join ` blocks indefinitely. You
31+ therefore **must close or dereference ** any active
32+ :class: `~pymongo.mongo_client.MongoClient ` before exiting.
3233
33- Code example, this function is called when your application is asked
34- to exit or gracefully reload itself by receiving a SIGHUP signal :
34+ An example solution to this issue in some application frameworks is a signal
35+ handler to end background greenlets when your application receives SIGHUP:
3536
3637.. code-block :: python
3738
3839 import signal
3940
4041 def graceful_reload (signum , traceback ):
41- """ Explicitly close our connection
42- """
42+ """ Explicitly close some global MongoClient object."""
4343 client.close()
4444
4545 signal.signal(signal.SIGHUP , graceful_reload)
4646
47- **uWSGI ** applications using the ``gevent-wait-for-hub `` option are directly
48- affected by this behaviour.
47+ Applications using uWSGI prior to 1.9.16 are affected by this issue,
48+ or newer uWSGI versions with the ``-gevent-wait-for-hub `` option.
49+ See `the uWSGI changelog for details
50+ <http://uwsgi-docs.readthedocs.org/en/latest/Changelog-1.9.16.html#important-change-in-the-gevent-plugin-shutdown-reload-procedure> `_.
51+
0 commit comments