~crunch.io/ubuntu/precise/pymongo/unstable

« back to all changes in this revision

Viewing changes to doc/examples/gevent.rst

  • Committer: Joseph Tate
  • Date: 2013-01-31 08:00:57 UTC
  • mfrom: (1.1.12)
  • Revision ID: jtate@dragonstrider.com-20130131080057-y7lv17xi6x8c1j5x
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
Gevent
2
 
===========================
3
 
 
4
 
.. testsetup::
5
 
 
6
 
  from pymongo import Connection, ReplicaSetConnection
7
 
 
8
 
PyMongo supports `Gevent <http://www.gevent.org/>`_. Primarily, this means that
9
 
:meth:`~pymongo.connection.Connection.start_request()` can ensure that the
 
2
======
 
3
 
 
4
PyMongo supports `Gevent <http://www.gevent.org/>`_. Simply call Gevent's
 
5
``monkey.patch_all()`` before loading any other modules:
 
6
 
 
7
.. doctest::
 
8
 
 
9
  >>> # You must call patch_all() *before* importing any other modules
 
10
  >>> from gevent import monkey; monkey.patch_all()
 
11
  >>> from pymongo import MongoClient
 
12
  >>> connection = MongoClient()
 
13
 
 
14
PyMongo's Gevent support means
 
15
that :meth:`~pymongo.mongo_client.MongoClient.start_request()` ensures the
10
16
current greenlet (not merely the current thread) uses the same socket for all
11
 
operations until :meth:`~pymongo.connection.Connection.end_request()` is called.
 
17
operations until :meth:`~pymongo.mongo_client.MongoClient.end_request()` is called.
12
18
See the :doc:`requests documentation <requests>` for details on requests in
13
19
PyMongo.
14
20
 
15
 
Using Gevent Without Threads
16
 
----------------------------
17
 
 
18
 
Typically when using Gevent, you will run ``from gevent import monkey;
19
 
monkey.patch_all()`` early in your program's execution. From then on, all
20
 
thread-related Python functions will act on `greenlets
21
 
<http://pypi.python.org/pypi/greenlet>`_ instead of threads, and PyMongo will
22
 
treat greenlets as if they were threads transparently. Each greenlet will use a
23
 
socket exclusively by default.
24
 
 
25
 
.. doctest::
26
 
 
27
 
  >>> from gevent import monkey; monkey.patch_all()
28
 
  >>> connection = Connection()
29
 
 
30
 
Make sure you're using the latest version of Gevent to ensure that
31
 
thread-locals are patched to act like greenlet-locals.
32
 
 
33
21
Using Gevent With Threads
34
22
-------------------------
35
23
 
36
24
If you need to use standard Python threads in the same process as Gevent and
37
 
greenlets, you only need to run ``monkey.patch_socket()``, rather than
 
25
greenlets, run ``monkey.patch_socket()``, rather than
38
26
``monkey.patch_all()``, and create a
39
 
:class:`~pymongo.connection.Connection` with ``use_greenlets=True``.
40
 
The :class:`~pymongo.connection.Connection` will use a special greenlet-aware
41
 
connection pool that allocates a socket for each greenlet, ensuring consistent
42
 
reads in Gevent.
 
27
:class:`~pymongo.mongo_client.MongoClient` with ``use_greenlets=True``.
 
28
The :class:`~pymongo.mongo_client.MongoClient` will use a special greenlet-aware
 
29
connection pool.
43
30
 
44
31
.. doctest::
45
32
 
46
33
  >>> from gevent import monkey; monkey.patch_socket()
47
 
  >>> connection = Connection(use_greenlets=True)
 
34
  >>> from pymongo import MongoClient
 
35
  >>> connection = MongoClient(use_greenlets=True)
48
36
 
49
 
An instance of :class:`~pymongo.replica_set_connection.ReplicaSetConnection`
 
37
An instance of :class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`
50
38
created with ``use_greenlets=True`` will also use a greenlet-aware pool.
51
39
Additionally, it will use a background greenlet instead of a background thread
52
40
to monitor the state of the replica set.
53
41
 
54
 
Using :meth:`~pymongo.replica_set_connection.ReplicaSetConnection.start_request()`
55
 
with :class:`~pymongo.ReadPreference` PRIMARY ensures that the current greenlet
56
 
uses the same socket for all operations until a call to :meth:`end_request()`.
57
 
 
58
 
You must `install Gevent <http://gevent.org/>`_ to use
59
 
:class:`~pymongo.replica_set_connection.ReplicaSetConnection`
60
 
with ``use_greenlets=True``
61
 
 
62
42
.. doctest::
63
43
 
64
44
  >>> from gevent import monkey; monkey.patch_socket()
65
 
  >>> rsc = ReplicaSetConnection(
 
45
  >>> from pymongo.mongo_replica_set_client import MongoReplicaSetClient
 
46
  >>> rsc = MongoReplicaSetClient(
66
47
  ...     'mongodb://localhost:27017,localhost:27018,localhost:27019',
67
48
  ...     replicaSet='repl0', use_greenlets=True)
 
49
 
 
50
Setting ``use_greenlets`` is unnecessary under normal circumstances; simply call
 
51
``patch_all`` to use Gevent with PyMongo.
 
 
b'\\ No newline at end of file'