-
Notifications
You must be signed in to change notification settings - Fork 108
[BUG] Support for ASGI startup events #911
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
Comments
After more digging in import logging
import azure.functions as func
from azure.functions._http_asgi import AsgiResponse, AsgiRequest
from api_fastapi import app
IS_INITED = False
async def run_setup(app):
"""Workaround to run Starlette startup events on Azure Function Workers."""
global IS_INITED
if not IS_INITED:
await app.router.startup()
IS_INITED = True
async def handle_asgi_request(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
asgi_request = AsgiRequest(req, context)
scope = asgi_request.to_asgi_http_scope()
asgi_response = await AsgiResponse.from_app(app, scope, req.get_body())
return asgi_response.to_func_response()
async def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
await run_setup(app)
return await handle_asgi_request(req, context) In Improve throughput performance of Python apps in Azure Functions, it is recommended to define the Shouldn't this then be the preferred way to run ASGI applications? |
I'm facing a similar problem. Based on https://github.com/ecerami/fastapi_azure I have create an Azure function with FastAPI inside. Everything works very smooth so far but my startup event handler is not run
|
This works for me too, but there is no way to perform the app global teardown operations (FastAPI shutdown event), since you have no hook on the running threadpool context of function worker. This is dirty if you have some channel open (like an open AIOHttp session) :( Note: You have to put this: global IS_INITED
if not IS_INITED:
await app.router.startup()
IS_INITED = True within a thread lock in order to avoid races |
I've run across a different issue with the AsgiMiddleware and arrived at basically the same solution as posted above. I noticed that simple async functions do not behave as I would expect an async application should. For example, I tested the following:
If you hit this endpoint above with a few concurrent requests, you'll notice that they execute serially instead of concurrently (I assume because the AsgiMiddleware blocks with a call to Using a plain azure function without the middleware works as expected (e.g., the above scenario completes in ~5 seconds with two concurrent requests):
As mentioned, the posted solution resolves this, but I'm concerned about the implications of this as mentioned above. It would be great to see a robust solution for this in the near future if possible. |
@rbudnar Yeah I'm struggling with this too Azure-Samples/fastapi-on-azure-functions#4 and it would be nice with an "official" solution and not a private method :-) #911 (comment) also avoids nest_asyncio |
@rbudnar This blocking problem is fixed now here Azure/azure-functions-python-library#143 which also removes the need for |
@ApurvaMisra maybe you would be able to put it inside an extension using |
Submitted a draft implementation of lifespan events that would work with this example (in the V2 programming model) |
Same issue here, any updates on that? |
Investigative information
Please provide the following:
Repro steps
Provide the steps required to reproduce the problem:
func/__init__.py
:Expected behavior
Provide a description of the expected behavior.
Actual behavior
Provide a description of the actual behavior observed.
Known workarounds
Provide a description of any known workarounds.
Contents of the requirements.txt file:
Provide the requirements.txt file to help us find out module related issues.
The text was updated successfully, but these errors were encountered: