Are there good reasons for Django using synchronous connection and cursors as opposed to async ones? We are thinking of extending Django with support for async connections and are wondering if sync-only is a design decision or just something that hasn’t been implemented yet.
Bonus question:
Why does Django enforce thread-locality of connections when psycopg connections are thread-safe?
Yes, thank you - that answered a lot. The only question remaining is whether or not it is possible to use atomic transactions for code that is async. Is it safe to enter an atomic block in a sync function and then start an event loop within that block? Will the asynchronous code respect the atomic transaction block? Does it work if one were to use async_to_sync?
I.e. would the following code from this post have desirable behaviour?
(I’ve been a bit checked out of this problem space, so things may have changed)
I think theoretically this could work, but I’m not sure it’s actually supported and not guaranteed to do so. And it might not be safe:
If the only asynchronous code running in an event loop inside your entire application takes place inside an atomic block then maybe this would work (as listed above). The problem comes in when you have any other async code running at the same time. Let’s say you have the following code:
asyncio.create_task(complex_job_with_db_usage_that_yields_several_times()) # 1
async with AsyncAtomicContextManager():
exists = await Model.objects.aget(...) # 2
if not exists:
await Model.objects.acreate(...) # 3
Task 1 executes first and is scheduled on the event loop. The next time control is yielded back to the event loop it will start to run. Then async task 2 happens, and while the database is processing our query due to the await control will be handed back to the asyncio loop to find something else to do. At this point, we’re supposedly inside an atomic context but suddenly task 1 starts running again! Since Django’s internals do not support async atomic block there is nothing stopping task 1 from reading and writing data that conflicts with the actions taken inside the atomic block. Then, by the time task 3 starts running the invariant we’re trying to uphold is no longer valid.
In summary, due to the complexities involved there isn’t a solution to this problem right now.