django ORM with asyncio

441 views
Skip to first unread message

luke lukes

unread,
Sep 18, 2018, 8:55:15 AM9/18/18
to Django users

I'm looking for something that can allow the usage of the Django ORM with asyncio (with PostgreSQL). 

As of now I found only aiopg in the asyncio ecosystem, which allows to run raw SQL or to use SQLAlchemy only.


I then found something in the django channels docs


is this:

https://github.com/django/channels/blob/master/channels/db.py


the missing piece that I was looking for?


Thanks

Andrew Godwin

unread,
Sep 18, 2018, 5:17:53 PM9/18/18
to django...@googlegroups.com
The Django ORM can't just be used with asyncio directly, sadly. The database_sync_to_async function you found there will help you call the ORM from asynchronous code, but it still runs the ORM in a synchronous thread (see https://channels.readthedocs.io/en/latest/topics/databases.html)

The project to make the ORM truly async is a tough one and yet to be started.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2af17ca1-6d48-45b0-9406-37e046934567%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

luke lukes

unread,
Sep 25, 2018, 3:09:47 PM9/25/18
to Django users
Hi Andrew,
thanks for your reply, well to be honest I'm a bit surprised / honored to get the answer directly from the creator of South.

Anyhow, aiopg is not the only option, I noticed that there are also asyncpg and aiopg8000.
My question is: 
even creating a new database wrapper, how can I modify the connection used by the ORM 'on-the-fly' (e.g. only during a management command, that would one of the few occasions where mixing AsyncIO with Django can be handy IMHO)? 
and that would be enough to use the ORM asynchronously? 

I mean if it's possible with SQLAlchemy there should be a way to achieve the same thing with the Django ORM...
Reply all
Reply to author
Forward
0 new messages