Improve lifespan control #2582
Replies: 3 comments 9 replies
-
This is the same problem I've been going round in circles for weeks. My code worked with the separate startup/shutdown functions but not with Command line arguments are passed to start up the system eg.
The arguments are validated in Instead an
|
Beta Was this translation helpful? Give feedback.
-
Wrt your proposed solution, how is it used? My configuration is simply:
Also, I use structlog as the logger and need to pass an error message from where the validation failed. Best ... |
Beta Was this translation helpful? Give feedback.
-
Have you tried running your own event loop? https://github.com/adriangb/asapi?tab=readme-ov-file#run-in-your-event-loop |
Beta Was this translation helpful? Give feedback.
-
Proposal
Provide means to better control the application lifetime.
e.g. provide exception classes similar to
SystemExit
to allow apps more control over their own lifetime.Background
I'm currently working on a small project using FastAPI, during the startup process I run some checks and if they are not fulfilled the startup has to fail.
While it is possible to just raise an exception this actually results in a huge unwanted traceback, when this is the expected behavior and I just need
Startup procedure failed, xyz not fulfilled
.I have solved this now by handling a special exception class in the
Router.lifespan
method by patching it on theRouter
instance created used by theFastAPI
instance.While this solution works for me, I believe this functionality should be build into Starlette.
My solution
Beta Was this translation helpful? Give feedback.
All reactions