Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async behavior will turn to blocking behavior while working with GzipMiddleWare #84

Closed
doglex opened this issue Nov 27, 2023 · 4 comments

Comments

@doglex
Copy link

doglex commented Nov 27, 2023

Workaround is sub application

@doglex
Copy link
Author

doglex commented Nov 27, 2023

demo

# main.py
app = FastAPI()
from sse import route as subapp
app.mount("/sse", subapp)
app1 = FastAPI()
app1.add_middleware(GZipMiddleware, minimum_size=1000)
app1.include_router(be.route, prefix="/be")
app.mount("/", app1)
# sse.py
from fastapi import APIRouter, Request, FastAPI
import asyncio
from sse_starlette.sse import EventSourceResponse
route = FastAPI()
@route.get("/s1")
async def message_stream(req: Request):
    async def event_publisher():
        i = 50
        try:
          while i:
              i -= 1
              # print(i)
              yield dict(data=i)
              await asyncio.sleep(0.5)
        except asyncio.CancelledError as e:
          print(f"Disconnected from client (via refresh/close) {req.client}")
          # Do any other cleanup, if any
          raise e
    return EventSourceResponse(event_publisher())

@sysid
Copy link
Owner

sysid commented Nov 27, 2023

@doglex, I am not sure whether I understand what you are trying to point out.

README states: Caveat: SSE streaming does not work in combination with encode/starlette#20 (comment).

Do you propose a solution/workaround for this?

@doglex
Copy link
Author

doglex commented Nov 28, 2023

Caveat

Yes, it is the issue

@sysid
Copy link
Owner

sysid commented Nov 28, 2023

If you have got a solution please open a PR. Would be much appreciated

@sysid sysid closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants