Hello there! I’m very new to Django’s internals, and something’s been troubling me with asynchronous support.
For context, I’m using Django along a discord.py
bot, meaning the context is always asynchronous, all my queries have to be done async.
Recently I’ve been trying to access the internal psycopg connection object to access the .copy
method, but I noticed the connection returned is synchronous, where AsyncConnection
exists for asynchronous queries. After further searching, I also noticed most of the asynchronous ORM methods are just the synchronous methods wrapped in sync_to_async
.
So here’s my question, how does Django manage async?
If the database backends calls are synchronous, how does Django actually achieve asynchronicity? I’ve ran some tests myself before, and long running queries do not block the asyncio event loop, so something must be happening for it to work, but I cannot understand where and how the magic happens.