Skip to content

Commit f2e9b04

Browse files
DOCSP-41887 Async Migration Guide (#103)
(cherry picked from commit acff81f)
1 parent 631d03e commit f2e9b04

File tree

6 files changed

+431
-1
lines changed

6 files changed

+431
-1
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
2424
[constants]
2525
driver-short = "PyMongo"
2626
driver-long = "PyMongo, the MongoDB synchronous Python driver,"
27+
driver-async = "PyMongo Async"
2728
language = "Python"
2829
mdb-server = "MongoDB Server"
2930
mongo-community = "MongoDB Community Edition"

source/connect/stable-api.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,3 @@ following API documentation:
120120
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__
121121
- `ServerApi <{+api-root+}pymongo/server_api.html#pymongo.server_api.ServerApi>`__
122122
- `ServerApiVersion <{+api-root+}pymongo/server_api.html#pymongo.server_api.ServerApiVersion>`__
123-
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. important::
2+
3+
The {+driver-async+} driver is experimental and should not be used in
4+
production environments. Classes, methods, and behaviors described in this
5+
guide might change prior to the full release. If you encounter any
6+
issues with {+driver-async+}, you can learn how to report them on the
7+
:ref:`pymongo-issues-help` page.

source/index.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ MongoDB {+driver-short+} Documentation
2626
/troubleshooting
2727
/whats-new
2828
/upgrade
29+
/motor-async-migration
30+
/pymongo-to-async-guide
2931
/previous-versions
3032
/issues-and-help
3133
/compatibility
@@ -120,6 +122,21 @@ Upgrade {+driver-short+} Versions
120122
Learn what changes you might need to make to your application to upgrade driver versions
121123
in the :ref:`pymongo-upgrade` section.
122124

125+
Migrate from Motor to {+driver-async+}
126+
-------------------------------------
127+
128+
In September 2024, MongoDB released the experimental {+driver-async+} driver as a replacement
129+
for `Motor <https://www.mongodb.com/docs/drivers/motor/>`__, the previous asynchronous
130+
MongoDB driver for Python. Learn how to migrate from Motor
131+
to the {+driver-async+} driver in the :ref:`pymongo-async-motor-migration`
132+
section.
133+
134+
Switch from {+driver-short+} to {+driver-async+}
135+
----------------------------------------------
136+
137+
Learn what changes you need to make to switch from {+driver-short+} to
138+
the experimental {+driver-async+} driver in the :ref:`pymongo-to-async-guide` section.
139+
123140
Previous Versions
124141
-----------------
125142

source/motor-async-migration.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _pymongo-async-motor-migration:
2+
3+
====================================
4+
Migrate from Motor to {+driver-async+}
5+
====================================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: motor, async, refactor, migration
19+
20+
.. include:: /includes/pymongo-async-experimental.rst
21+
22+
Overview
23+
--------
24+
25+
The {+driver-async+} driver is a unification of {+driver-short+} and the `Motor
26+
library <https://www.mongodb.com/docs/drivers/motor/>`__. In this guide, you can
27+
identify the changes you must make to migrate an application from
28+
Motor to the {+driver-async+} driver.
29+
30+
Migrate From Motor
31+
------------------
32+
33+
The {+driver-async+} driver functions similarly to the Motor library, but allows
34+
for improved latency and throughput due to directly using Python Asyncio instead
35+
of delegating work to a thread pool. In most cases, you can directly migrate
36+
existing Motor applications to {+driver-async+} by using ``AsyncMongoClient`` in
37+
place of ``MotorClient``, and changing the application's import statements to
38+
import from ``pymongo``.
39+
40+
The following example shows the difference in imports to use a client for
41+
read and write operations in Motor compared to {+driver-async+}:
42+
43+
.. code-block:: python
44+
45+
# Motor client import
46+
from motor.motor_asyncio import AsyncIOMotorClient
47+
48+
# {+driver-async+} client import
49+
from pymongo import AsyncMongoClient
50+
51+
To see a list of the asynchronous methods available in the {+driver-async+}
52+
driver, see the :ref:`pymongo-async-methods` section in the {+driver-short+} to
53+
{+driver-async+} guide.
54+
55+
The following section shows the method signature changes that you must implement
56+
in your application when migrating from Motor to the {+driver-async+} driver.
57+
58+
Method Signature Changes
59+
~~~~~~~~~~~~~~~~~~~~~~~~
60+
61+
The following Motor method signatures behave differently in the {+driver-async+} driver:
62+
63+
- ``AsyncMongoClient.__init__()`` does not accept an ``io_loop`` parameter.
64+
- ``AsyncCursor.each()`` does not exist in the {+driver-async+} driver.
65+
- ``MotorGridOut.stream_to_handler()`` does not exist in the {+driver-async+} driver.
66+
- ``AsyncCursor.to_list(0)`` is not valid in the {+driver-async+} driver. Use
67+
``to_list(None)`` instead.
68+
- ``MongoClient`` is thread safe and can be used by many threads, however, an
69+
``AsyncMongoClient`` is not thread safe and should only be used by a single
70+
event loop.
71+
72+
Additional Information
73+
----------------------
74+
75+
To learn more about asynchronous Python, see the `Python Asyncio documentation
76+
<https://docs.python.org/3/library/asyncio.html>`__.

0 commit comments

Comments
 (0)