'Break response when client goes from stream (StreamingHttpResponse) (Django, Google Cloud Run)
I am using StreamingHttpResponse to pass binary data to the client. First, I need to set the start and end times of the stream. Locally, when the client leaves the stream (I close the browser page), the close() method is triggered.
From django code (from HttpResponseBase class):
# These methods partially implement the file-like object interface.
# See https://docs.python.org/library/io.html#io.IOBase
# The WSGI server must call this method upon completion of the request.
# See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html
def close(self):
for closer in self._resource_closers:
try:
closer()
except Exception:
pass
# Free resources that were still referenced.
self._resource_closers.clear()
self.closed = True
signals.request_finished.send(sender=self._handler_class)
On Google Cloud Run, this method is called only after 300 seconds (regardless of whether the client is listening to the stream or not). Is it possible to call the close() method when the client leaves the stream on the server?
I expected that the response would be interrupted on the server as soon as the client leaves the stream (web page). It worked locally.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
