Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing a blocking request in django view

In one of the views in my django application, I need to perform a relatively lengthy network IO operation. The problem is other requests must wait for this request to be completed even though they have nothing to do with it. I did some research and stumbled upon Celery but as I understand, it is used to perform background tasks independent of the request. (so I can not use the result of the task for the response to the request)

Is there a way to process views asynchronously in django so while the network request is pending other requests can be processed?

Edit: What I forgot to mention is that my application is a web service using django rest framework. So the result of a view is a json response not a page that I can later modify using AJAX.

like image 843
ali mostafavi Avatar asked Nov 08 '22 21:11

ali mostafavi


1 Answers

The usual solution here is to offload the task to celery, and return a "please wait" response in your view. If you want, you can then use an Ajax call to periodically hit a view that will report whether the response is ready, and redirect when it is.

like image 73
Daniel Roseman Avatar answered Nov 14 '22 22:11

Daniel Roseman