|
| 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