Reputation: 7322
I just want to clarify few things.
I believe that Django server works asynchronously (because if every request from the client would block server then it wouldn't work), but I know also that the Django ORM isn't async. So do queries to the database block the server? (I mean that rest requests are waiting until the query is complete?) Or maybe it works completely differently and I misunderstood it.
I'm asking this because I heard that most ORMs are blocking and hence I can't use them in my Twisted server to get data from db without blocking twisted.
Upvotes: 8
Views: 6359
Reputation: 1123400
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).
Upvotes: 9
Reputation: 1792
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).
Upvotes: 0