django icon indicating copy to clipboard operation
django copied to clipboard

Support async database backends

Open domingues opened this issue 8 months ago • 1 comments

Hello, first of all I apologize if this is not the appropriate place to have this discussion, if not please direct me to the correct place.

This is a try to start discussing the ability yo have asynchronous backends (e.g. psycopg3) and thus being able to use the ORM asynchronously. This proposal is backwards compatible.

Use the django.db.backends.postgresql_async backend to try, do not use runserver, not all Query methods where migrated but these will work:

  • prefetch_related
  • acount
  • afirst
  • alast
  • async for

The approach was:

  • added foundations: BaseDatabaseWrapperAsync, CursorWrapperAsync, CursorDebugWrapperAsync - same interface but with async methods when needed;
  • migrated PostgresSQL backend to psycopg3 async interface;
  • migrate Query async methods to use the new async interface;
  • call async_to_sync on sync methods when using an async backend;
  • call sync_to_async on async methods when using a sync backend;

Does not investigated yet:

  • runserver shenanigans;
  • migrations;

Problems:

  • is created one database connection for each operation done on a sync view when using an async backend. That is expected since database connections are "bind" to threads+coroutines. To fix this we will need to find a way to "bind" the connections to a theard+request, that should also facilitate the implementation of connection pools (https://github.com/django/django/pull/16881);
  • for the same motives of previous point, the connections are not closed. On async views the workaround, which is not always feasible, is closing the connection at the end of the view;
  • ?

Ty for your feedback.

domingues avatar Sep 18 '23 09:09 domingues

@domingues Have you seen this? https://github.com/django/deps/blob/main/accepted/0009-async.rst#id13

It is a useful source to learn what the maintainers planned for async ORM.

salomvary avatar Oct 05 '23 13:10 salomvary