How to detect connection interruption in Django?

I have a Django view that does pretty heavy processing and takes about 20-30 seconds to return the result.

Sometimes the user finishes closing the browser window (terminating the connection) before completing the request - in this case, I would like to detect this and stop working. The work I do is read-only in the database, so there are no transaction problems.

In PHP, the connection_aborted function does just that. Is this feature available in Django?

Here is an example of the code I would like to write:

def myview(request): while not connection_aborted(): # do another bit of work... if work_complete: return HttpResponse('results go here') 

Thanks.

+4
source share
3 answers

I do not think that Django provides it because it basically cannot. More than Django, it depends on how Django interacts with your web server. It all depends on your software stack (which you did not specify). I do not think that this is even part of the FastCGI and WSGI protocols!

Change I'm also sure that Django does not start sending any data to the client until your view has completed execution, so it cannot know if the connection was dead. The main socket will not cause an error if the server does not try to send some data to the user.

+1
source

This connection_aborted method in PHP doesn't work the way you think. It will tell you if the client is disconnected, but only if the buffer has been flushed, i.e. Some kind of response is sent from the server back to the client. PHP versions will not work, as you wrote, if above. You will need to add a call to something like flush in your loop so that the server tries to send data.

HTTP is a stateless protocol. It is designed so that the client or server does not depend on each other. As a result, the state is either known only when the connection was created, and this only happens when some data is sent one way or another.

It’s best to do what @MattH suggested and do it through a bit of AJAX, and if you want to integrate something like Node.js to make the client “check-ins” during processing. However, how to correctly establish this, I am not in my field of knowledge.

0
source

So, you have an AJAX view that launches a request, which takes 20-30 seconds to process the request against the background of the displayed page, and you are worried about lost resources when someone cancels the page loading.

I see that you have options in three broad categories:

  • Live with him. Improve the situation by copying the results if the user returns.
  • Make it faster. Throw more space during / space. Maintaining staging tables. Pre-calculate everything, etc.
  • Do something smart with a quick browser poll, "is it still ready?" the request and the server that cancels the request if it does not receive nag in the interval * 2 or similar. If you are really smart, you can return progress / ETA to nags. However, this may not have particularly useful behavior when the system is under load, or your site is accessed for limited bandwidth.

I don’t think you should go for option 3, because it increases the complexity and use of resources for a small gain.

-1
source

All Articles