[Python] Problem with proxying multiple video streams via FastApi

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have a large backend app on python + fastapi. There are many different endpoints, including one that is used to proxy video streams. The usage is something like this - the endpoint receives the video stream URL, opens it and gives it via streaming response. If I proxy 5 video streams, then everything is fine. If I proxy 6 streams, then my python app stops accepting any rest requests until I close one of the open streams in the browser. I will need to proxy a much larger number of video streams, but the problem occurs already when proxying 6. How fix this?

    I am attaching a minimally reproducible example, but I cannot attach the links to the video streams themselves. I will note that there are no problems with the original streams, they work directly in any quantity.

    import uvicorn
    import httpx
    from fastapi import FastAPI
    from fastapi.responses import StreamingResponse

    app = FastAPI()


    async def proxy_mjpeg_stream(url: str):

    async with httpx.AsyncClient() as client:
    try:
    async with client.stream("GET", url) as stream:
    async for chunk in stream.aiter_bytes():
    if chunk:
    yield chunk
    except httpx.ReadTimeout as ER:
    print(f'ERROR ReadTimeout - {url=} - {ER}')


    @app.get("/get_stream")
    async def get_stream(url: str):
    if url:
    headers = {
    "Content-Type": 'multipart/x-mixed-replace; boundary=frame',
    }
    return StreamingResponse(proxy_mjpeg_stream(url), headers=headers)


    if __name__ == "__main__":
    uvicorn.run(app)

    Continue reading...

Compartilhe esta Página