Aller au contenu

Sync ORM in async context (ARCH2043)

Ce contenu n’est pas encore disponible dans votre langue.

Key python.async.sync-orm-in-async · Severity warning · Category async

A synchronous ORM/DB call (Django ORM, SQLAlchemy sync Session) inside an async coroutine blocks the event loop and stalls all concurrency. Use an async driver or offload to a thread.

async def get_user(user_id):
return User.objects.get(id=user_id) # sync ORM, blocks the loop
from asgiref.sync import sync_to_async
async def get_user(user_id):
return await sync_to_async(User.objects.get)(id=user_id)

Use the async ORM API, or move the sync call off the loop with sync_to_async/an executor.