Skip to content

Commit bdc7b75

Browse files
authored
DOCSP-48161: Async examples for Connect pages (#236)
1 parent 0b4adeb commit bdc7b75

File tree

12 files changed

+584
-94
lines changed

12 files changed

+584
-94
lines changed

source/connect.txt

Lines changed: 178 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,118 @@ 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

75+
The following sections describe how to connect to different targets, such as a local
76+
instance of MongoDB or a cloud-hosted instance on Atlas.
77+
5878
Local Deployment
5979
~~~~~~~~~~~~~~~~
6080

61-
.. code-block:: python
81+
The following code shows how to connect the connection string to connect to a local
82+
MongoDB deployment. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to
83+
see the corresponding code:
6284

63-
uri = "mongodb://localhost:27017/"
64-
client = MongoClient(uri)
85+
.. tabs::
86+
87+
.. tab:: Synchronous
88+
:tabid: sync
89+
90+
.. code-block:: python
91+
92+
uri = "mongodb://localhost:27017/"
93+
client = MongoClient(uri)
94+
95+
.. tab:: Asynchronous
96+
:tabid: async
97+
98+
.. code-block:: python
99+
100+
uri = "mongodb://localhost:27017/"
101+
client = AsyncMongoClient(uri)
65102

66103
Atlas
67104
~~~~~
68105

69-
.. code-block:: python
106+
The following code shows the connection string to connect to a deployment hosted on
107+
Atlas. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
108+
corresponding code:
70109

71-
uri = "<Atlas connection string>"
72-
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
73-
version="1", strict=True, deprecation_errors=True))
110+
.. tabs::
111+
112+
.. tab:: Synchronous
113+
:tabid: sync
114+
115+
.. code-block:: python
116+
117+
uri = "<Atlas connection string>"
118+
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
119+
version="1", strict=True, deprecation_errors=True))
120+
121+
.. tab:: Asynchronous
122+
:tabid: async
123+
124+
.. code-block:: python
125+
126+
uri = "<Atlas connection string>"
127+
client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi(
128+
version="1", strict=True, deprecation_errors=True))
74129

75130
Replica Set
76131
~~~~~~~~~~~
77132

78-
.. code-block:: python
133+
The following code shows the connection string to connect to a replica set. Select the
134+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
135+
code:
136+
137+
.. tabs::
138+
139+
.. tab:: Synchronous
140+
:tabid: sync
141+
142+
.. code-block:: python
143+
144+
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
145+
client = MongoClient(uri)
79146

80-
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
81-
client = MongoClient(uri)
147+
.. tab:: Asynchronous
148+
:tabid: async
149+
150+
.. code-block:: python
151+
152+
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
153+
client = AsyncMongoClient(uri)
82154

83155
Network Compression
84156
-------------------
85157

158+
The following sections describe how to connect to MongoDB while specifying network
159+
compression algorithms.
160+
86161
Compression Algorithms
87162
~~~~~~~~~~~~~~~~~~~~~~
88163

@@ -94,6 +169,8 @@ To learn more about specifying compression algorithms, see
94169
zlib Compression Level
95170
~~~~~~~~~~~~~~~~~~~~~~
96171

172+
The following tabs demonstrate how to specify a compression level for the ``zlib`` compressor:
173+
97174
.. tabs::
98175

99176
.. tab:: MongoClient
@@ -102,8 +179,8 @@ zlib Compression Level
102179
.. code-block:: python
103180

104181
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
105-
compressors = "zlib",
106-
zlibCompressionLevel=<zlib compression level>)
182+
compressors = "zlib",
183+
zlibCompressionLevel=<zlib compression level>)
107184

108185
.. tab:: Connection String
109186
:tabid: connectionstring
@@ -115,29 +192,84 @@ zlib Compression Level
115192
"zlibCompressionLevel=<zlib compression level>")
116193
client = pymongo.MongoClient(uri)
117194

195+
.. tab:: MongoClient (Asynchronous)
196+
:tabid: mongoclient-async
197+
198+
.. code-block:: python
199+
200+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
201+
compressors = "zlib",
202+
zlibCompressionLevel=<zlib compression level>)
203+
204+
.. tab:: Connection String (Asynchronous)
205+
:tabid: connectionstring-async
206+
207+
.. code-block:: python
208+
209+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
210+
"compressors=zlib"
211+
"zlibCompressionLevel=<zlib compression level>")
212+
client = pymongo.AsyncMongoClient(uri)
213+
118214
To learn more about setting the zlib compression level, see
119215
:ref:`pymongo-enable-compression` in the Network Compression guide.
120216

121217
Server Selection
122218
----------------
123219

124-
.. code-block:: python
220+
The following code shows a connection string that specifies a server selection function.
221+
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
222+
code:
125223

126-
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
127-
server_selector=<selector function>)
224+
.. tabs::
225+
226+
.. tab:: Synchronous
227+
:tabid: sync
228+
229+
.. code-block:: python
230+
231+
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
232+
server_selector=<selector function>)
233+
234+
.. tab:: Asynchronous
235+
:tabid: async
236+
237+
.. code-block:: python
238+
239+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
240+
server_selector=<selector function>)
128241

129242
To learn more about customizing server selection, see
130243
:ref:`pymongo-server-selection`.
131244

132245
{+stable-api+}
133246
--------------
134247

135-
.. code-block:: python
248+
The following code shows how to specify {+stable-api+} settings for a connection.Select the
249+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
250+
code:
136251

137-
from pymongo.server_api import ServerApi
252+
.. tabs::
138253

139-
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
140-
server_api=ServerApi("<{+stable-api+} version>"))
254+
.. tab:: Synchronous
255+
:tabid: sync
256+
257+
.. code-block:: python
258+
259+
from pymongo.server_api import ServerApi
260+
261+
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
262+
server_api=ServerApi("<{+stable-api+} version>"))
263+
264+
.. tab:: Asynchronous
265+
:tabid: async
266+
267+
.. code-block:: python
268+
269+
from pymongo.server_api import ServerApi
270+
271+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
272+
server_api=ServerApi("<{+stable-api+} version>"))
141273

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

@@ -147,6 +279,8 @@ Limit Server Execution Time
147279
timeout Block
148280
~~~~~~~~~~~~~
149281

282+
The following code shows how to set a client-side timeout by using the ``timeout()`` method:
283+
150284
.. code-block:: python
151285

152286
with pymongo.timeout(<timeout length>):
@@ -157,6 +291,9 @@ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
157291
timeoutMS Connection Option
158292
~~~~~~~~~~~~~~~~~~~~~~~~~~~
159293

294+
The following tabs demonstrate how to set a client-side timeout by using the ``timeoutMS``
295+
connection option:
296+
160297
.. tabs::
161298

162299
.. tab:: MongoClient
@@ -165,7 +302,7 @@ timeoutMS Connection Option
165302
.. code-block:: python
166303

167304
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
168-
timeoutMS=<timeout length>)
305+
timeoutMS=<timeout length>)
169306

170307
.. tab:: Connection String
171308
:tabid: connectionstring
@@ -175,5 +312,20 @@ timeoutMS Connection Option
175312
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
176313
client = pymongo.MongoClient(uri)
177314

178-
To learn more about client-side timeouts, see :ref:`pymongo-csot`.
315+
.. tab:: MongoClient (Asynchronous)
316+
:tabid: mongoclient-async
317+
318+
.. code-block:: python
319+
320+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
321+
timeoutMS=<timeout length>)
322+
323+
.. tab:: Connection String (Asynchronous)
324+
:tabid: connectionstring-async
179325

326+
.. code-block:: python
327+
328+
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
329+
client = pymongo.AsyncMongoClient(uri)
330+
331+
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
------------------

0 commit comments

Comments
 (0)