-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Sub application startup event never triggered #811
Comments
Digging in to this a bit more, I can see that FastAPI merely exposes the Starlette lifecycle events and doesn't do anything special about them. In addition, this issue has been raised previously against Starlette. Since it's deemed out-of-scope by Starlette developers, I think the only action here would be to document this in the FastAPI docs i.e. call out explicitly that sub-application event handlers are ignored. |
I also think it would be good to document this. PR welcome! |
Thanks for the investigation and for reporting back @jonathanunderwood , yep, it makes sense to have it documented. |
I found this issue this days, so what should I do to trigger subapp's start_up callbacks? |
@ClericPy it would have to be implemented in Starlette: encode/starlette#649 |
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
As an update, looks like there's activity upstream in Starlette to bring this capability in: encode/starlette#1988 |
some updates about this feature, sometimes it is required specially when dealing with non-fastapi ASGI apps like socketio one https://github.com/miguelgrinberg/python-socketio/blob/main/src/socketio/asgi.py |
Yes, But there is an easy work around for this for now.
I find this super useful for certain cases. For example if I am making an app which has one AI model loaded on each router I wouldnt be able to add features to them like socketio. So for now it appears that having a fastapi instance for such use cases and mounting it is ideal. Paired with the above mentioned startup function hack and its doable... |
What I did was to create a function to mount my application and register my event handlers in the parent app def mount_app(parent_app: FastAPI, path: str) -> None:
parent_app.on_event("startup")(app_startup)
parent_app.on_event("shutdown")(app_shutdown)
parent_app.mount(path, app) |
I did something similar. Provide a reference for others. from contextlib import AsyncExitStack, asynccontextmanager
from fastapi import FastAPI
from starlette.routing import Mount
@asynccontextmanager
async def lifespan(app: FastAPI):
async with AsyncExitStack() as stack:
for route in app.routes:
if isinstance(route, Mount) and isinstance(route.app, FastAPI):
await stack.enter_async_context(
route.app.router.lifespan_context(route.app), # noqa
)
yield |
Describe the bug
Startup event handlers for sub-applications never trigger.
To Reproduce
Running the above shows:
$ python subapp_events.py INFO: Started server process [38462] INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: Waiting for application startup. ++++ App startup event INFO: Application startup complete.
Notice that
sub_app_startup()
is never run.Expected behavior
I would expect the sub-application startup event handler to also run (although I am not sure what ordering I would expect).
Environment
Additional context
N/A
The text was updated successfully, but these errors were encountered: