Are concurrent database queries in ASGI a thing?

Hi folks,

I recently started experimenting with asynchronous views on an existing codebase to improve response times where the view rendering depends on the results of dozens of database queries.

I’m past optimizing the database queries themselves and the logical next step was to try executing (at least some of) them concurrently.

So I’ve switched to Daphne ASGI server and started adding async views and making database queries concurrently using await asyncio.gather(....).

However when I look at the query execution timeline in Sentry, the database queries appear to happen strictly sequentially.

I understand that asynchronous does not necessary mean concurrent but I was hoping that Django + ASGI will bring both. I also re-read the relevant documentation here but I am left clueless: Asynchronous support | Django documentation | Django

I’m using psycopg2-binary = "^2.9.6" with django = "^4.2".

Any insights would be welcome.

Cheers,
Márton

I got the bottom of this myself, and the answer is, no, as of 5.0, Django can not execute database queries concurrently.

Now I understand that implementing a truly asynchronous database backend is a huge undertaking. The only thing I found is this work in progress pull request, but it seems to be in a very early stage: Support async database backends by domingues · Pull Request #17275 · django/django · GitHub