@@ -17,32 +17,35 @@ Gevent's monkey-patching replaces those standard functions so that PyMongo
17
17
does asynchronous I/O with non-blocking sockets, and schedules operations
18
18
on greenlets instead of threads.
19
19
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
+ --------------------------
24
22
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.
27
27
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.
32
33
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:
35
36
36
37
.. code-block :: python
37
38
38
39
import signal
39
40
40
41
def graceful_reload (signum , traceback ):
41
- """ Explicitly close our connection
42
- """
42
+ """ Explicitly close some global MongoClient object."""
43
43
client.close()
44
44
45
45
signal.signal(signal.SIGHUP , graceful_reload)
46
46
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