-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-41887 Async Migration Guide #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOCSP-41887 Async Migration Guide #103
Conversation
✅ Deploy Preview for docs-pymongo ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a few things!
source/index.txt
Outdated
Migrate to the {+driver-async+} Driver | ||
------------------------------------ | ||
|
||
Learn how to migrate from {+driver-short+} or Motor to the {+driver-async+} driver in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: I think another sentence here could be useful before linking to the guide. Maybe you can mention the time period that this applies to?
Eg.
In Aug 2024, MongoDB released the PyMongo Async driver to unify PyMongo and Motor, the asynchronous MongoDB driver for Python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flagging this again, but also ok if you wanted to leave as is!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, misread this the first time and applied it to the wrong area. Fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approving as comments arent blocking but happy to re-review if needed!
source/index.txt
Outdated
Migrate to the {+driver-async+} Driver | ||
------------------------------------ | ||
|
||
Learn how to migrate from {+driver-short+} or Motor to the {+driver-async+} driver in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flagging this again, but also ok if you wanted to leave as is!
source/pymongo-async-migration.txt
Outdated
* - ``AsyncMongoClient()`` | ||
- .. code-block:: python | ||
|
||
from pymongo import AsyncMongoClient | ||
|
||
client = AsyncMongoClient(...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: perhaps this should have its own subsection as its a constructor rather than a method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that at first, but it seemed weird to be in its own table and it's related to the client so I figured it made sense here. Can change if anyone has strong feelings the other way though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First pass, I'll take another look tomorrow morning for a deeper dive on the technical details.
source/index.txt
Outdated
|
||
In September 2024, MongoDB released the {+driver-async+} driver to unify {+driver-short+} | ||
and `Motor <https://www.mongodb.com/docs/drivers/motor/>`__, the asynchronous | ||
MongoDB driver for Python. Learn how to migrate from {+driver-short+} or Motor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be clear that the new Async API is a replacement for Motor specifically and that any existing users of PyMongo will only have to migrate if they want to switch to async.
source/pymongo-async-migration.txt
Outdated
|
||
The {+driver-async+} driver is a unification of {+driver-short+} and the `Motor | ||
library <https://www.mongodb.com/docs/drivers/motor/>`__. In this guide, you can | ||
identify the changes you must make to migrate an application from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, the word "migrate" seems a little confusing for existing PyMongo users. Is there a different wording we can use to make sure users understand that they only need to make changes if they want to use the new async API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworded this after splitting pages
source/pymongo-async-migration.txt
Outdated
- Add the ``await`` keyword to all asynchronous method calls. | ||
- If an asynchronous method is called within a function, mark the function as ``async``. | ||
|
||
The following sections describe how to implement the asynchronous API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also link to the official Python async docs here for some additional context.
source/pymongo-async-migration.txt
Outdated
|
||
from pymongo import AsyncMongoClient | ||
|
||
AsyncMongoClient(...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To use an AsyncMongoClient
as a context manager users will have to do async with AsyncMongoClient(...)
instead of with MongoClient(...)
.
source/pymongo-async-migration.txt
Outdated
Migrate From Motor | ||
------------------ | ||
|
||
The {+driver-async+} driver behaves nearly identically to the Motor library. In |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little misleading. Async PyMongo is (or should be) vastly superior to Motor in behavior both in terms of latency and throughput because Async Pymongo directly uses asyncio whereas Motor delegates all work to a thread pool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the wording here, let me know if that works better!
source/pymongo-async-migration.txt
Outdated
|
||
await collection.find_one_and_update(...) | ||
|
||
Migrate From Motor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see the motor -> async pymongo migration as its own dedicated page. The pymongo sync -> async differences clutters the page for motor users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split the pages into two
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One clarifying suggestion, otherwise looks good!
source/pymongo-to-async-guide.txt
Outdated
|
||
- Replace all uses of ``MongoClient`` with ``AsyncMongoClient``. | ||
- Add the ``await`` keyword to all asynchronous method calls. | ||
- If an asynchronous method is called within a function, mark the function as ``async``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unclear that this last point is for functions that the user defines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. Updated
@ShaneHarvey any other suggested changes on your end before I merge? |
source/index.txt
Outdated
Migrate from Motor to {+driver-async+} | ||
------------------------------------- | ||
|
||
In September 2024, MongoDB released the {+driver-async+} driver as a replacement |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Async PyMongo is released yes, but it's only in beta (ie experimental) and not ready for production use. We should make this loud and clear one every one of these pages.
source/motor-async-migration.txt
Outdated
|
||
- ``GridOut.open()`` returns ``None``. | ||
- ``AsyncMongoClient.__init__()`` does not accept an ``io_loop`` parameter. | ||
- ``GridIn.set()`` does not accept a filename. Instead pass a file name by using the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@noah is this true? Does GridIn
here refer to a motor class? The pymongo class is AsyncGridIn
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AsyncGridIn.set()
does accept filenames.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were from the original draft migration notes linked in the ticket. Should I delete the GridIn
bullets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
source/motor-async-migration.txt
Outdated
|
||
The following Motor method signatures behave differently in the {+driver-async+} driver: | ||
|
||
- ``GridOut.open()`` returns ``None``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does GridOut
here refer to a motor class? The pymongo class is AsyncGridOut
.
source/motor-async-migration.txt
Outdated
- ``AsyncMongoClient.__init__()`` does not accept an ``io_loop`` parameter. | ||
- ``GridIn.set()`` does not accept a filename. Instead pass a file name by using the | ||
``GridIn.filename`` attribute. | ||
- ``Cursor.each()`` does not exist in the {+driver-async+} driver. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor -> AsyncCursor.
source/motor-async-migration.txt
Outdated
- ``GridIn.set()`` does not accept a filename. Instead pass a file name by using the | ||
``GridIn.filename`` attribute. | ||
- ``Cursor.each()`` does not exist in the {+driver-async+} driver. | ||
- ``stream_to_handler()`` does not exist in the {+driver-async+} driver. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MotorGridOut.stream_to_handler()
source/motor-async-migration.txt
Outdated
``GridIn.filename`` attribute. | ||
- ``Cursor.each()`` does not exist in the {+driver-async+} driver. | ||
- ``stream_to_handler()`` does not exist in the {+driver-async+} driver. | ||
- ``to_list(0)`` is not valid in the {+driver-async+} driver. Use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AsyncCursor.to_list(0)
|
||
- Replace all uses of ``MongoClient`` with ``AsyncMongoClient``. | ||
- Add the ``await`` keyword to all asynchronous method calls. | ||
- If you call an asynchronous method inside of a function, mark the function as ``async``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add:
- A ``MongoClient`` is thread safe and can be used be many threads. A ``AsyncMongoClient`` is not thread safe and should only be used by a single event loop.
(cherry picked from commit acff81f)
(cherry picked from commit acff81f)
Pull Request Info
PR Reviewing Guidelines
JIRA - https://jira.mongodb.org/browse/DOCSP-41887
Staging -
https://deploy-preview-103--docs-pymongo.netlify.app/motor-async-migration/
https://deploy-preview-103--docs-pymongo.netlify.app/pymongo-to-async-guide
Self-Review Checklist