Skip to content

Commit fe6f7de

Browse files
committed
DOCSP-48161: Async examples for Connect pages
1 parent 8185060 commit fe6f7de

File tree

12 files changed

+546
-91
lines changed

12 files changed

+546
-91
lines changed

source/connect.txt

Lines changed: 141 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,95 @@ the relevant values for your MongoDB deployment.
4646

4747
.. include:: /includes/usage-examples/sample-app-intro.rst
4848

49-
.. literalinclude:: /includes/usage-examples/connect-sample-app.py
50-
:language: python
51-
:copyable: true
52-
:linenos:
53-
:emphasize-lines: 4-6
49+
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
50+
code:
51+
52+
.. tabs::
53+
54+
.. tab:: Synchronous
55+
:tabid: sync
56+
57+
.. literalinclude:: /includes/usage-examples/connect-sample-app.py
58+
:language: python
59+
:copyable: true
60+
:linenos:
61+
:emphasize-lines: 4-6
62+
63+
.. tab:: Asynchronous
64+
:tabid: async
65+
66+
.. literalinclude:: /includes/usage-examples/connect-sample-app-async.py
67+
:language: python
68+
:copyable: true
69+
:linenos:
70+
:emphasize-lines: 6-8
5471

5572
Connection
5673
----------
5774

5875
Local Deployment
5976
~~~~~~~~~~~~~~~~
6077

61-
.. code-block:: python
78+
.. tabs::
6279

63-
uri = "mongodb://localhost:27017/"
64-
client = MongoClient(uri)
80+
.. tab:: Synchronous
81+
:tabid: sync
82+
83+
.. code-block:: python
84+
85+
client = MongoClient("mongodb://localhost:27017/")
86+
87+
.. tab:: Asynchronous
88+
:tabid: async
89+
90+
.. code-block:: python
91+
92+
uri = "mongodb://localhost:27017/"
93+
client = AsyncMongoClient(uri)
6594

6695
Atlas
6796
~~~~~
6897

69-
.. code-block:: python
98+
.. tabs::
7099

71-
uri = "<Atlas connection string>"
72-
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
73-
version="1", strict=True, deprecation_errors=True))
100+
.. tab:: Synchronous
101+
:tabid: sync
102+
103+
.. code-block:: python
104+
105+
uri = "<Atlas connection string>"
106+
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
107+
version="1", strict=True, deprecation_errors=True))
108+
109+
.. tab:: Asynchronous
110+
:tabid: async
111+
112+
.. code-block:: python
113+
114+
uri = "<Atlas connection string>"
115+
client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi(
116+
version="1", strict=True, deprecation_errors=True))
74117

75118
Replica Set
76119
~~~~~~~~~~~
77120

78-
.. code-block:: python
121+
.. tabs::
122+
123+
.. tab:: Synchronous
124+
:tabid: sync
125+
126+
.. code-block:: python
127+
128+
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
129+
client = MongoClient(uri)
79130

80-
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
81-
client = MongoClient(uri)
131+
.. tab:: Asynchronous
132+
:tabid: async
133+
134+
.. code-block:: python
135+
136+
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
137+
client = AsyncMongoClient(uri)
82138

83139
Network Compression
84140
-------------------
@@ -115,29 +171,76 @@ zlib Compression Level
115171
"zlibCompressionLevel=<zlib compression level>")
116172
client = pymongo.MongoClient(uri)
117173

174+
.. tab:: MongoClient (Asynchronous)
175+
:tabid: mongoclient-async
176+
177+
.. code-block:: python
178+
179+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
180+
compressors = "zlib",
181+
zlibCompressionLevel=<zlib compression level>)
182+
183+
.. tab:: Connection String (Asynchronous)
184+
:tabid: connectionstring-async
185+
186+
.. code-block:: python
187+
188+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
189+
"compressors=zlib"
190+
"zlibCompressionLevel=<zlib compression level>")
191+
client = pymongo.AsyncMongoClient(uri)
192+
118193
To learn more about setting the zlib compression level, see
119194
:ref:`pymongo-enable-compression` in the Network Compression guide.
120195

121196
Server Selection
122197
----------------
123198

124-
.. code-block:: python
199+
.. tabs::
125200

126-
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
127-
server_selector=<selector function>)
201+
.. tab:: Synchronous
202+
:tabid: sync
203+
204+
.. code-block:: python
205+
206+
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
207+
server_selector=<selector function>)
208+
209+
.. tab:: Asynchronous
210+
:tabid: async
211+
212+
.. code-block:: python
213+
214+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
215+
server_selector=<selector function>)
128216

129217
To learn more about customizing server selection, see
130218
:ref:`pymongo-server-selection`.
131219

132220
{+stable-api+}
133221
--------------
134222

135-
.. code-block:: python
223+
.. tabs::
136224

137-
from pymongo.server_api import ServerApi
225+
.. tab:: Synchronous
226+
:tabid: sync
138227

139-
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
140-
server_api=ServerApi("<{+stable-api+} version>"))
228+
.. code-block:: python
229+
230+
from pymongo.server_api import ServerApi
231+
232+
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
233+
server_api=ServerApi("<{+stable-api+} version>"))
234+
235+
.. tab:: Asynchronous
236+
:tabid: async
237+
238+
.. code-block:: python
239+
240+
from pymongo.server_api import ServerApi
241+
242+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
243+
server_api=ServerApi("<{+stable-api+} version>"))
141244

142245
To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.
143246

@@ -175,5 +278,20 @@ timeoutMS Connection Option
175278
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
176279
client = pymongo.MongoClient(uri)
177280

178-
To learn more about client-side timeouts, see :ref:`pymongo-csot`.
281+
.. tab:: MongoClient (Asynchronous)
282+
:tabid: mongoclient-async
179283

284+
.. code-block:: python
285+
286+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
287+
timeoutMS=<timeout length>)
288+
289+
.. tab:: Connection String (Asynchronous)
290+
:tabid: connectionstring-async
291+
292+
.. code-block:: python
293+
294+
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
295+
client = pymongo.AsyncMongoClient(uri)
296+
297+
To learn more about client-side timeouts, see :ref:`pymongo-csot`.

source/connect/connection-options.txt

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,26 @@ Using the Connection URI
4242
If you pass a connection URI to the ``MongoClient`` constructor, you can include
4343
connection options in the string as ``<name>=<value>`` pairs. In the following example,
4444
the connection URI contains the ``connectTimeoutMS`` option with a value of ``60000``
45-
and the ``tls`` option with a value of ``true``:
45+
and the ``tls`` option with a value of ``true``. Select the :guilabel:`Synchronous` or
46+
:guilabel:`Asynchronous` tab to see the corresponding code:
4647

47-
.. code-block:: python
48+
.. tabs::
4849

49-
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
50-
client = pymongo.MongoClient(uri)
50+
.. tab:: Synchronous
51+
:tabid: sync
52+
53+
.. code-block:: python
54+
55+
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
56+
client = pymongo.MongoClient(uri)
57+
58+
.. tab:: Asynchronous
59+
:tabid: async
60+
61+
.. code-block:: python
62+
63+
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
64+
client = pymongo.AsyncMongoClient(uri)
5165

5266
.. _pymongo-mongo-client-settings:
5367

@@ -59,12 +73,26 @@ instead of including them in your connection URI.
5973
Configuring the connection this way makes it easier to
6074
change settings at runtime and helps you catch errors during compilation.
6175
The following example shows how to use the ``MongoClient`` constructor to set
62-
connection options:
76+
connection options. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to
77+
see the corresponding code:
78+
79+
.. tabs::
80+
81+
.. tab:: Synchronous
82+
:tabid: sync
83+
84+
.. code-block:: python
85+
86+
uri = "mongodb://<hostname>:<port>"
87+
client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)
88+
89+
.. tab:: Asynchronous
90+
:tabid: async
6391

64-
.. code-block:: python
92+
.. code-block:: python
6593

66-
uri = "mongodb://<hostname>:<port>"
67-
client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)
94+
uri = "mongodb://<hostname>:<port>"
95+
client = pymongo.AsyncMongoClient(uri, connectTimeoutMS=60000, tls=True)
6896

6997
Connection Options
7098
------------------

source/connect/connection-options/connection-pools.txt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,43 @@ your connection URI:
110110
| **Connection URI Example**: ``waitQueueTimeoutMS=100000``
111111

112112
The following code creates a client with a maximum connection pool size of ``50`` by using the
113-
``maxPoolSize`` parameter:
113+
``maxPoolSize`` parameter. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous`
114+
tab to see the corresponding code:
114115

115-
.. code-block:: python
116+
.. tabs::
116117

117-
client = MongoClient(host, port, maxPoolSize=50)
118+
.. tab:: Synchronous
119+
:tabid: sync
120+
121+
.. code-block:: python
122+
123+
client = MongoClient(host, port, maxPoolSize=50)
124+
125+
.. tab:: Asynchronous
126+
:tabid: async
127+
128+
.. code-block:: python
129+
130+
client = AsyncMongoClient(host, port, maxPoolSize=50)
118131

119132
The following code creates a client with the same configuration as the preceding example,
120133
but uses a connection URI:
121134

122-
.. code-block:: python
135+
.. tabs::
136+
137+
.. tab:: Synchronous
138+
:tabid: sync
139+
140+
.. code-block:: python
141+
142+
client = MongoClient(host, port, maxPoolSize=50)
143+
144+
.. tab:: Asynchronous
145+
:tabid: async
146+
147+
.. code-block:: python
123148

124-
client = MongoClient("mongodb://<host>:<port>/?maxPoolSize=50")
149+
client = AsyncMongoClient(host, port, maxPoolSize=50)
125150

126151
Additional Information
127152
----------------------

source/connect/connection-options/network-compression.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ The following code example specifies the ``zlib`` compression algorithm and a va
9393
"zlibCompressionLevel=1")
9494
client = pymongo.MongoClient(uri)
9595

96+
.. tab:: MongoClient (Asynchronous)
97+
:tabid: mongoclient-async
98+
99+
.. code-block:: python
100+
:emphasize-lines: 2-3
101+
102+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
103+
compressors = "zlib",
104+
zlibCompressionLevel=1)
105+
106+
.. tab:: Connection String (Asynchronous)
107+
:tabid: connectionstring-async
108+
109+
.. code-block:: python
110+
:emphasize-lines: 2-3
111+
112+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
113+
"compressors=zlib"
114+
"zlibCompressionLevel=1")
115+
client = pymongo.AsyncMongoClient(uri)
116+
96117
API Documentation
97118
-----------------
98119

source/connect/connection-options/server-selection.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,27 @@ the list of servers originally passed as an argument:
117117
return servers
118118

119119
Finally, instruct {+driver-short+} to use your function. To do so, call the ``MongoClient``
120-
constructor and pass the ``server_selector`` argument with your function name as the value:
120+
constructor and pass the ``server_selector`` argument with your function name as the value.
121+
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
122+
code:
121123

122-
.. code-block:: python
124+
.. tabs::
125+
126+
.. tab:: Synchronous
127+
:tabid: sync
128+
129+
.. code-block:: python
130+
131+
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
132+
server_selector=prefer_local)
133+
134+
.. tab:: Asynchronous
135+
:tabid: async
136+
137+
.. code-block:: python
123138

124-
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
125-
server_selector=prefer_local)
139+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
140+
server_selector=prefer_local)
126141

127142
API Documentation
128143
-----------------

0 commit comments

Comments
 (0)