Docs Menu

PyMongo 비동기로 마이그레이션

중요

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

PyMongo 비동기 운전자 PyMongo 와 모터 라이브러리를 통합한 것입니다. 이 가이드 에서는 애플리케이션 을 PyMongo 또는 모터 에서 PyMongo 비동기 운전자 로 마이그레이션 위해 수행해야 하는 변경 사항을 확인할 수 있습니다.

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

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

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

PyMongo 비동기 운전자 에서 사용할 수 있는 비동기 메서드 목록을 보려면 PyMongo PyMongo 가이드 의 비동기 메서드 섹션을 참조하세요.

다음 섹션에서는 모터 에서 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 은 스레드로부터 안전하지 않으며 여러 스레드에서 사용할 수 있지만 은 스레드로부터 안전하지 않으므로 단일 이벤트 루프 에서만 사용해야 합니다.

PyMongo 비동기 운전자 PyMongo 와 유사하게 동작하지만 네트워크 작업을 수행하는 모든 메서드는 코루틴이므로 대기해야 합니다. PyMongo 에서 PyMongo 비동기로 마이그레이션 하려면 다음과 같은 방법으로 코드를 업데이트 해야 합니다.

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