-
Background:
I don't think we've been explicit yet as to what the planned future state is for lifespan and on_startup / on_shutdown. We currently have two mutually exclusive ways to do things, which is confusing for users and adds complexity to our codebase (see #2068 for how much complexity). Using async context managers is (as far as I can tell) never worse than on_startup / on_shutdown and makes some use cases much easier (if you need to wrap another context manager like Given that using async context managers are all around better, I propose we get rid of on_startup and on_shutdown for 1.0. What do other think? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 13 replies
-
what foot guns are you talking about, I admit I didnt read entirely the thread, but I never had any issue with on_startup and on_shutdown |
Beta Was this translation helpful? Give feedback.
-
I also didn't read the whole thread, but based on what I read, the lifespan state would solve the issue I think most of our users use |
Beta Was this translation helpful? Give feedback.
-
Funny story... 😬 It's quite a contradiction from my side, but anyway, here it goes.
Then, the next steps would be that I would have to patch If anyone wants arguments from my side for my point of view I can expand on it. But as I suspect I would be the main expected "opposition" to this (given my contradictions above) and I'm actually in favor, maybe I can avoid making this message longer. Edit 2023-03-07: I just released FastAPI 0.93.0, with support for (I also scratched above the sections that are no longer true/necessary). Here are the updated docs: https://fastapi.tiangolo.com/advanced/events/ |
Beta Was this translation helpful? Give feedback.
-
I'm still not convinced it's a good idea. 🤷 But... If we want to proceed with this, we should:
With this, FastAPI doesn't need to do anything. Let's wait for a week or so to see if any other member is strong about not doing this. cc @encode/maintainers |
Beta Was this translation helpful? Give feedback.
-
The main footgun is forgetting/failing to teardown your ressources. Your code will work no matter how you initialize your resources, but there are plenty of "fun" ways it will break if you don't deinitialize them afterwards. It's the main reason why Enforcing the CM approach gives the user more ways to properly handle teardown, such as relying on object-provided context managers to have the objects be deinitialized automatically, or a Besides compatibility with applications that already use † From my understanding, |
Beta Was this translation helpful? Give feedback.
-
this one i quite real and even if there are multiple good ways to grab your hand and avoid making those mistakes, like when using pytest with the "right" config, if this promotes better default behavior I'm all in favor of the proposed switch |
Beta Was this translation helpful? Give feedback.
-
I was looking for this issue, using |
Beta Was this translation helpful? Give feedback.
-
I am moving away from fastapi. I am a bit confused. I wanted to check database connection if they are healthy how do I pass request aka app.state.database reference to a lifecycle function in Starlette? In fastapi I do something like:
in Starlette instead of app.pool i have app.state.pool |
Beta Was this translation helpful? Give feedback.
Funny story... 😬 It's quite a contradiction from my side, but anyway, here it goes.
I currently don't have support forI do havelifespan
functions in FastAPI, but I should (there might even be a PR for that).startup
andshutdown
.on_startup
andon_shutdown
. And replace that with a singlelifespan
context manager/function withyield
. 😬Then, the next steps would be that I would have to patch
on_startup
andon_shutdown
in FastAPI for backwards compatibility for a while,add support for. And I think all that should be done, and I agree with remov…lifespan
, document it, recommend it, discourage usingon_startup
andon_shutdown
and direct people tolifespan