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

Ensure generators are closed #218

Open
pgjones opened this issue Feb 7, 2023 · 1 comment
Open

Ensure generators are closed #218

pgjones opened this issue Feb 7, 2023 · 1 comment

Comments

@pgjones
Copy link
Member

pgjones commented Feb 7, 2023

import asyncio
from quart import Quart, Response

app = Quart(__name__)

@app.route('/')
async def sub():
        response = Response(
            data_stream(),
            mimetype="text/event-stream",
        )
        response.timeout = None
        return response

async def data_stream():
    try:
        while True:
            print("Sending Data")
            if False:
                yield "data: {ok: true}\n\n"
            await asyncio.sleep(1)
    finally:
        print("Exited Gen")
    
app.run()
@sungeer
Copy link

sungeer commented Apr 5, 2024

ha, please follow me :

import asyncio
from quart import Quart, make_response, stream_with_context
from datetime import datetime

app = Quart(__name__)

@app.route('/')
async def sub():
    @stream_with_context
    async def data_stream():
        try:
            while True:
                print("Sending Data")
                yield "data: {ok: true}\n\n".encode()
                await asyncio.sleep(1)
        finally:
            print("Exited Gen")

    response = await make_response(data_stream())
    response.timeout = None
    response.headers['Content-Type'] = 'text/event-stream'
    return response

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

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