Gevent

PyMongo supports Gevent. Primarily, this means that start_request() can ensure that the current greenlet (not merely the current thread) uses the same socket for all operations until end_request() is called. See the requests documentation for details.

Using Gevent Without Threads

Typically when using Gevent, you will run from gevent import monkey; monkey.patch_all() early in your program’s execution. From then on, all thread-related Python functions will act on greenlets instead of threads, and PyMongo will treat greenlets as if they were threads transparently. Each greenlet will use a socket exclusively by default.

>>> from gevent import monkey; monkey.patch_all()
>>> connection = Connection()

Make sure you’re using the latest version of Gevent to ensure that thread-locals are patched to act like greenlet-locals.

Using Gevent With Threads

If you need to use standard Python threads in the same process as Gevent and greenlets, you can run only monkey.patch_socket(), and create a Connection with use_greenlets=True. The Connection will use a special greenlet-aware connection pool that allocates a socket for each greenlet, ensuring consistent reads in Gevent.

>>> from gevent import monkey; monkey.patch_socket()
>>> connection = Connection(use_greenlets=True)

Table Of Contents

Previous topic

Python 3 FAQ

Next topic

Requests

This Page