deathangel908
deathangel908

Reputation: 9689

django 3.0 async orm

Since django 3.0 supports async I wonder what happens on database querying. I don't see any updates in the official documentation which I'm sure that syntax like this:

b5.name = 'New name'
b5.save()

Will totally block the current thread, which event loop is running it. And If the database is returning the response for 20 seconds, no other request would be processed during that time which is nasty.

Another thing that makes me wonder: async uses only 1 thread (if we don't await things in thread-executor at least). Here's the fact: an atomic (database) request is bound to the connection of the database, which is bound to the thread-local variable. Which means all the requests would run in the single thread = all of them would have the same transaction.

Upvotes: 3

Views: 5569

Answers (2)

deathangel908
deathangel908

Reputation: 9689

Finally, since 3 years Django 4.2 released on 3 May with async support for querying models

async def make_book(*args, **kwargs):
    book = Book(...)
    await book.asave(using="secondary")

Upvotes: 0

felocru
felocru

Reputation: 23

As Willem says, it is asynchronous by request. The difference of asgi, is that in a single request it can be resolved faster, since also within the request it is asynchronous. For example when consulting on the BD or consulting a resource on the web. It is true that Django 3.0 still lacks to be totally asynchronous, for you to use asynchronous benefits you have to make use of the asgi library, with the methods: sync_to_async and async_to_sync. Review this documentation so you have an idea of how it works: https://channels.readthedocs.io/en/latest/topics/databases.html

In the same way, I share this league that explains very well all the differences, of parallelism, asynchronous, threads. etc. https://fastapi.tiangolo.com/async/#asynchronous-code

Upvotes: 1

Related Questions