django icon indicating copy to clipboard operation
django copied to clipboard

[WIP] Async ORM

Open nicolaslara opened this issue 5 years ago • 1 comments

I've started experimenting with allowing the ORM to make async calls to the DB. I am very slowly, but surely making progress.

This is far from ready, but I wanted to open this PR to provide visibility into what I'm doing.

My next steps are more or less:

  • General cleanup and making sure things work both for the sync and async cases
  • Allow this to work via DefaultConnectionProxy
  • Experiment with how to use this from a higher level perspective. Does the current approach (connection.a.cursor().execute()) even make sense?
  • Consider the possibility of the ORM using async internally in a way that is invisible to the user.

Once that is done, I'll be in a position to start scoping out the work needed to move this forward and I'll post to the forum to get some discussions going (and hopefully recruit someone to help me with it)

(Some of my personal notes about the work can be found here: https://github.com/nicolaslara/django/blob/async_orm/orm_notes.md)


At the moment, this is implemented:

>>> from django.db import connections
>>> conn = connections.all()[0]
>>> await conn.a.connect()
>>> async with conn.a.cursor() as cur:
...    await cur.execute('select 1')
...    print(await cur.fetchall())
[(1,)]

>>> cur = await conn.a.cursor()
>>> await cur.execute('select 1')
>>> await cur.fetchall()
[(1,)]

nicolaslara avatar Oct 31 '19 16:10 nicolaslara

@auvipy I was planning to work on this before covid, but I've been pretty low on time this year unfortunately, so I'm not sure any of the work on this branch is something we can reuse; it was more of an initial experiment at integrating an async db backend.

There are probably easier ways to do this: implement an async backend with minimal changes to the internal backends, or start by buidling the async interface while wrapping the actual calls in sync_to_async. Hopefully I'll have time to look at this again in the near future, but if I do, it will probably differ from this branch.

nicolaslara avatar Dec 08 '20 07:12 nicolaslara