Docs 菜单

迁移到PyMongo Async

重要

PyMongo异步驾驶员是实验性的,不应在生产环境中使用。 在发布完整发布之前,本指南中描述的类、方法和行为可能会发生变化。 如果您在使用PyMongo Async 时遇到任何问题,可以在问题和帮助页面上学习;了解如何报告这些问题。

The PyMongo Async driver is a unification of PyMongo and the Motor library. In this guide, you can identify the changes you must make to migrate an application from PyMongo or Motor to the PyMongo Async driver.

PyMongo异步驾驶员的功能与Motor库类似,但由于直接使用Python Asyncio 而不是将工作委托给线程池,因此可以改善延迟和吞吐量。 在大多数情况下,您可以使用 AsyncMongoClient 代替 MotorClient 并将应用程序的导入语句更改为从 pymongo 导入,从而将现有Motor应用程序直接迁移到PyMongo Async。

以下示例显示了在Motor中与PyMongo Async 中使用客户端进行写入操作的导入差异:

# Motor client import
from motor.motor_asyncio import AsyncIOMotorClient
# PyMongo Async client import
from pymongo import AsyncMongoClient

To see a list of the asynchronous methods available in the PyMongo Async driver, see the 异步方法 section in the PyMongo to PyMongo Async guide.

以下部分介绍了从Motor迁移到PyMongo异步驾驶员程序时必须在应用程序中实现的方法签名更改。

以下Motor方法签名在PyMongo异步驾驶员中的行为有所不同:

  • AsyncMongoClient.__init__() 不接受 io_loop 参数。

  • PyMongo异步驾驶员中不存在 AsyncCursor.each()

  • PyMongo异步驾驶员中不存在 MotorGridOut.stream_to_handler()

  • AsyncCursor.to_list(0) 在PyMongo异步驾驶员中无效。请改用 to_list(None)

  • MongoClient 具有线程安全性,可供多个线程使用,但 AsyncMongoClient 并非线程安全的,只能由单个事件循环使用。

The PyMongo Async driver behaves similarly to PyMongo, but all methods that perform network operations are coroutines and must be awaited. To migrate from PyMongo to PyMongo Async, you must update your code in the following ways:

  • 将所有使用的 MongoClient 替换为 AsyncMongoClient

  • 为所有异步方法调用添加 await 关键字。

  • 如果在函数内部调用异步方法,请将该函数标记为 async

以下部分介绍如何实现异步API。

下表列出了PyMongo异步驾驶员中可用的异步方法。 要调用这些方法,您必须对其进行 await 操作并在 async 函数内进行调用。

方法
例子

AsyncMongoClient()

from pymongo import AsyncMongoClient
async with AsyncMongoClient(...)

watch()

async with await client.watch(...) as stream:
...

server_info()

await client.server_info(...)

list_databases()

await client.list_databases()

list_database_names()

await client.list_database_names()

drop_database()

await client.drop_database(...)
方法
例子

watch()

async with await db.watch(...) as stream:
...

create_collection()

await db.create_collection(...)

aggregate()

async with await client.admin.aggregate(...) as cursor:
...

command()

await db.command(...)

cursor_command()

await db.cursor_command(...)

list_collections()

await db.list_collections()

list_collection_names()

await db.list_collection_names()

drop_collection()

await db.drop_collection(...)

validate_collection()

await db.validate_collection(...)

dereference()

await db.dereference(...)
方法
例子

watch()

async with await collection.watch(...) as stream:
...

insert_one()

await collection.insert_one(...)

insert_many()

await collection.insert_many(...)

replace_one()

await collection.replace_one(...)

update_one()

await collection.update_one(...)

update_many()

await collection.update_many(...)

drop()

await collection.drop()

delete_one()

await collection.delete_one(...)

delete_many()

await collection.delete_many(...)

find_one()

await collection.find_one(...)

estimated_document_count()

await collection.estimated_document_count()

count_documents()

await collection.count_documents(...)

create_index()

await collection.create_index(...)

create_indexes()

await collection.create_indexes(...)

drop_index()

await collection.drop_index(...)

drop_indexes()

await collection.drop_indexes()

list_indexes()

await collection.list_indexes()

index_information()

await collection.index_information()

list_search_indexes()

await collection.list_search_indexes()

create_search_index()

await collection.create_search_index(...)

create_search_indexes()

await collection.create_search_indexes(...)

drop_search_index()

await collection.drop_search_index(...)

update_search_index()

await collection.update_search_index(...)

options()

await collection.options()

aggregate()

async for doc in await collection.aggregate(...):
...

aggregate_raw_batches()

async for batch in await collection.aggregate_raw_batches(...):
...

rename()

await collection.rename(...)

distinct()

await collection.distinct(...)

find_one_and_delete()

await collection.find_one_and_delete(...)

find_one_and_replace()

await collection.find_one_and_replace(...)

find_one_and_update()

await collection.find_one_and_update(...)

要学习;了解有关异步Python的更多信息,请参阅Python Asyncio 文档。