Skip to content

Not able to stop StreamingResponse once the stream is started #4146

@sheikzaidh

Description

@sheikzaidh

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi.applications import FastAPI
import uvicorn
from fastapi.requests import Request
from fastapi.responses import StreamingResponse
from visitor_camera_fastapi import VideoCameraV


app = FastAPI()


async def genv(camera, request):
    while True:
        print(f'is disconnect {request._is_disconnected}')
        frame = camera.get_frame()
        yield (b'--frame\r\n'
            b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.get("/getCameraStream")
async def detectEmployees(request:Request):
    # body = await request.body()
    query = request.query_params
    return StreamingResponse(genv(VideoCameraV(request)), media_type='multipart/x-mixed-replace; boundary=frame')

Description

hi, In my project we capture the stream in the backend and passing to the front-end through API. If the front-end stop getting the stream .then also in the backend stream is getting pass. how Can I stop this stream

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.63.0

Python Version

3.7

Additional Context

I am getting the socket.send() error .there is way I can check the client is disconnect . I have tried with request._is_disconnected also but it returing false if the client disconnected also

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions