Django, calling an event after StreamingHttpResponse is closed

67 views Asked by At

Here is my code:

    def stream(self,request: HttpRequest,user,camera_index):
        try:
            url = self.config["cameras"][int(camera_index)]["url"]
        except IndexError or TypeError:
            return HttpResponseNotFound()

        response = StreamingHttpResponse(self.__stream(url),content_type="multipart/x-mixed-replace;boundary=frame")
        return response
    
    def __stream(self,url):
        video_cap = cv2.VideoCapture(url)
        while True:
            _, frame = cv2.imencode('.jpg', video_cap.read()[1],[int(cv2.IMWRITE_JPEG_QUALITY), 100])
            yield(b'--frame\r\n'
            b'Content-Type: image/jpeg\r\n\r\n' + frame.tobytes() + b'\r\n\r\n')

First, I want to know whether the __stream function is ended if user closes the page. I did some tests about it but I am not sure. Second, can I add a callback to __stream function that will be called when streaming is closed?

0

There are 0 answers