Do Django ORM database queries block the server, or are they asynchronous? Do Django ORM database queries block the server, or are they asynchronous? database database

Do Django ORM database queries block the server, or are they asynchronous?


Why would the server need to work asynchronously? Django is a WSGI application; the concurrency model depends on the server you run it in, and that can be threading, multiprocessing, asynchronous (select loop driven) or a combo of those.

Each Django request itself is completely synchronous. Querying the database blocks the request until the result is returned. It doesn't need to be aware of other, concurrent requests (other than ensuring that Django handles data structures in a thread-safe manner).


I've been facing the similar problem as you seem to be having. My django application performs a lot of calls to rest services to render the view and it bothered me they had to be serialized. I developed this:

https://github.com/kowalski/featdjango/

This is a application server based on twisted web. Unlike the django-on-twisted project it doesn't use the wsgi at all. Django code is run in a thread. There is a pool of them. Twisted code runs in the main application thread and manages the pool. If you need to do a few calls from the Django code and can benefit from doing it simultaneously you need to create a method, which returns a Deferred (or DeferredList). Than, from django code, you can call it by:

import threading...ct = threading.current_thread()result = ct.wait_for_defer(method_to_call, *args, **kwargs)

This has the effect of calling the *method_to_call* with reactor.callFromThread() method and binding the callbacks to wake up the caller thread. The result of the Deferred is returned, or the exception is raised (in case when the errback() is fired).