Skip to content
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

Feat: Add support for fastapi lifespan events #110

Open
robTheBuildr opened this issue May 23, 2024 · 5 comments
Open

Feat: Add support for fastapi lifespan events #110

robTheBuildr opened this issue May 23, 2024 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@robTheBuildr
Copy link

🚀 Feature

Add the ability for developers to customize the FastAPI lifespan, startup, and 'shutdown` event handlers.

Motivation

Users can use this to setup logging or resource management.

Pitch

There are 2 options here.

  1. Add a function attach_event_handler(event: str, func: Callable) which takes either 'lifespan', 'startup' or 'shutdown' as the event and applies the given logic.

  2. More explicit and abstracting away FastAPI Lifespan event knowledge from the user, add during_lifespan(func: Callable), on_startup(func: Callable), and on_shutdown(func: Callable) that the user can call on Server to apply these lifespan handlers.

Alternatives

Add custom template startup and shutdown event handler for logging and resource management. This will get cumbersome.

Additional context

@robTheBuildr robTheBuildr added enhancement New feature or request help wanted Extra attention is needed labels May 23, 2024
Copy link

Hi! thanks for your contribution!, great first issue!

@aniketmaurya
Copy link
Collaborator

hi @robTheBuildr, we are using the FastAPI lifespan to manage our parallel inference workers. May I know more about your use case and what you are trying to achieve with lifespan?

@williamFalcon
Copy link
Contributor

@robTheBuildr what will be helpful is a reproducible code snippet we can run that shows something you are trying to do, but not able to do with litserve

@aniketmaurya aniketmaurya added good first issue Good for newcomers and removed good first issue Good for newcomers labels May 31, 2024
@vrdn-23
Copy link

vrdn-23 commented Aug 28, 2024

@williamFalcon @aniketmaurya Hi! I'm new to the project and this seems interesting to our team for the use-cases we are working on.

what will be helpful is a reproducible code snippet we can run that shows something you are trying to do, but not able to do with litserve

In our case, we would essentially like to be able to add custom middleware to the FastAPI object so that we can track internal metrics and other things that is necessary for our infra.

An example of how that would look like is this:

_app = app or FastAPI()
_app.add_middleware(TrackingIdMiddleware)
_app.add_middleware(InstrumentedMiddleware)
_app.add_middleware(AccessLoggingMiddleware)
_app.add_middleware(ProxyHeadersMiddleware)

I'm not sure if it's possible to also allow users to pass in their own FastAPI app that has it's own lifespan and then append a lifespan that the LitServer uses to it.

@aniketmaurya
Copy link
Collaborator

hi @vrdn-23, you can track the progress for adding middleware in #228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants