[Solved]-Performing non-blocking requests? – Django


not out of the box as you’ve already returned out of the method. You could use something like Celery which would pass the do_more_stuff task onto a queue and then have it run do_more_stuff() outside of http request / response flow.



Django lets you accomplish this with Signals, more information can be found here. (Please note, as I said in comments below, signals aren’t non-blocking, but they do allow you to execute code after returning a response in a view.)

If you’re looking into doing many, many asynchronous requests and need them to be non-blocking, you may want to check out Tornado.



Because you’re returning from the function, do_more_stuff will never be called.

If you’re looking at doing heavy lifting stuff queuing up something before you return as Ross suggests (+1 for Celery).

if however you’re looking at returning some content… then doing something and returning more content to the user streaming is probably what you’re looking for. You can pass an iterator or a generator to HttpResponse, and it’ll iterate and push out the content in a trickle fashion. It feels a bit yuck, but if you’re a generator rockstar you may be able to do enough in various states to accomplish what you want.

Or I guess you could simply redesign your page to use a lot of ajax to do what you need, including firing off events to django views, reading data from views, etc.

It kind of comes down to where the burden of async is going to sit: client, server or response.

I’m not that familiar with node.js yet, but it would be interesting to see the use case you’re talking about.

EDIT: I did a little more looking into signals, and while they do occur in process, there is a built in signal for request_finished after the request has been handled by django, though it’s more of a catchall than something specific.

Leave a comment