ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

PyMongo 비동기로 마이그레이션

중요

PyMongo 비동기 운전자 는 실험용이므로 프로덕션 환경에서는 사용해서는 안 됩니다. 이 가이드 에 설명된 클래스, 메서드 및 동작은 전체 출시하다 이전에 변경될 수 있습니다. PyMongo 비동기에 문제가 발생하는 경우 문제 및 도움말 페이지에서 문제를 보고하는 방법을 학습 수 있습니다.

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 비동기 운전자 는 모터 라이브러리와 유사하게 PyMongo Motor 작동하지만,Python 스레드 풀에 작업을 위임하는 대신 Python Asyncio를 직접 사용하므로 지연 시간 과 처리량 이 향상됩니다. 대부분의 Motor 경우 대신 PyMongo 를 AsyncMongoClient 사용하고 MotorClient 애플리케이션의 가져오기 문을 에서 가져오도록 변경하여 기존 모터 애플리케이션을 PyMongo Async로 직접 마이그레이션 할 수 pymongo 있습니다.

다음 예시 는 모터 에서 읽기 및 쓰기 (write) 작업에 Motor PyMongo 클라이언트 를 사용할 경우 PyMongo 비동기와 비교했을 때 가져오기의 차이점을 보여줍니다.

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

다음 섹션에서는 모터 에서 PyMongo 비동기 운전자 로 마이그레이션할 때 애플리케이션 에서 구현 해야 하는 메서드 서명 변경 사항을 Motor 보여줍니다.PyMongo

다음 Motor PyMongo 모터 메서드 서명은 PyMongo 비동기 운전자 에서 다르게 작동합니다.

  • AsyncMongoClient.__init__() 은(는) io_loop 매개변수를 허용하지 않습니다.

  • AsyncCursor.each() 이(가) PyMongo 비동기 운전자 에 존재하지 않습니다.

  • MotorGridOut.stream_to_handler() 이(가) PyMongo 비동기 운전자 에 존재하지 않습니다.

  • 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 설명서를 참조하세요.