Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

Streaming python outputs, like #1426 #1460

Closed alexanderankin closed 1 month ago

alexanderankin commented 3 months ago

My question is the same as #1426, but i want to confirm this use case will be covered:

addressing the limitation in this line of code: b''.join(self._buffer)

https://github.com/Azure/azure-functions-python-library/blob/31f3d426fb0942af256ab4c8b348d580c274134a/azure/functions/_http_wsgi.py#L137-L145

    def to_func_response(self) -> HttpResponse:
        lowercased_headers = {k.lower(): v for k, v in self._headers.items()}
        return HttpResponse(
            body=b''.join(self._buffer),
            status_code=self._status_code,
            headers=self._headers,
            mimetype=lowercased_headers.get('content-type'),
            charset=lowercased_headers.get('content-encoding')
        )

e.g. for this use case: https://github.com/alexanderankin/flask-app-on-azure-functions/commit/4010b67b8acf45ac839383e10dcdb01d6f17f164

import time
from flask import Flask, Response
from .package.module import MODULE_VALUE
app = Flask(__name__)
def generate_numbers():
    """Generator function to yield numbers 1 to 10 with a 1-second delay."""
    for number in range(1, 11):
        yield f"data: {number}\n\n"
        time.sleep(1)
@app.route('/')
def index():
    """Serve the SSE stream."""
    return Response(generate_numbers(), content_type='text/event-stream')
if __name__ == "__main__":
    app.run()
import azure.functions as func
from FlaskApp import app
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.WsgiMiddleware(app.wsgi_app).handle(req, context)
YunchuWang commented 2 months ago

@alexanderankin thanks for reaching out! yes http streaming is supported in next function runtime version release. stay tuned

YunchuWang commented 1 month ago

python function http streaming is in public preview, for more details please refer to https://azure.microsoft.com/en-us/updates/public-preview-azure-functions-support-for-http-streams-in-python/

alexanderankin commented 1 month ago

Amazing thank you!

On Tue, May 21, 2024, 4:02 PM wangbill @.***> wrote:

python function http streaming is in public preview, for more details please refer to https://azure.microsoft.com/en-us/updates/public-preview-azure-functions-support-for-http-streams-in-python/

— Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-python-worker/issues/1460#issuecomment-2123350947, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACECGJA52EORJWSG275FBTLZDOR57AVCNFSM6AAAAABFSQ7W5SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRTGM2TAOJUG4 . You are receiving this because you were mentioned.Message ID: @.***>