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

✨ Add support for lifespan async context managers (superseding startup and shutdown events) #2944

Merged
merged 31 commits into from Mar 7, 2023

Conversation

uSpike
Copy link
Contributor

@uSpike uSpike commented Mar 13, 2021

This adds support for the lifespan argument to the application and router, which was added in starlette.

I tidied up some of the event tests as well to promote reuse.

Fixes #2943

@github-actions
Copy link
Contributor

📝 Docs preview for commit 8c33c94 at: https://604c4976cfdb60814a49803c--fastapi.netlify.app

@codecov
Copy link

codecov bot commented Mar 13, 2021

Codecov Report

Patch coverage: 100.00% and no project coverage change

Comparison is base (cf73051) 100.00% compared to head (12e924d) 100.00%.

❗ Current head 12e924d differs from pull request most recent head 6adb554. Consider uploading reports for the commit 6adb554 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #2944     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files          540       243    -297     
  Lines        13969      7441   -6528     
===========================================
- Hits         13969      7441   -6528     
Impacted Files Coverage Δ
fastapi/routing.py 100.00% <ø> (ø)
fastapi/applications.py 100.00% <100.00%> (ø)
tests/test_router_events.py 100.00% <100.00%> (ø)
fastapi/params.py 100.00% <0.00%> (ø)
fastapi/encoders.py 100.00% <0.00%> (ø)
tests/test_query.py 100.00% <0.00%> (ø)
fastapi/responses.py 100.00% <0.00%> (ø)
fastapi/exceptions.py 100.00% <0.00%> (ø)
fastapi/websockets.py 100.00% <0.00%> (ø)
... and 342 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 12e924d at: https://604c4d9c53c6988fd2efed45--fastapi.netlify.app

@nesb1
Copy link

nesb1 commented Oct 21, 2021

Hi, we really need this fix, can i help with PR? There is a conflict @uSpike

@meshantz meshantz mentioned this pull request Nov 5, 2021
@meshantz
Copy link
Contributor

meshantz commented Nov 5, 2021

I rebased, dealt with the commit conflicts and updated a test in #4147

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Nov 5, 2021

@meshantz There's no need for that. If @uSpike is not able to rebase, @tiangolo will eventually see this PR and rebase himself. 😗

@meshantz
Copy link
Contributor

meshantz commented Nov 5, 2021

One of the tests that was added was failing due to a starlette update. Saw that nothing had happened in the last 15 days, and was hoping to lend a hand.

I'd also like to see this get implemented, as it's much nicer than referencing the global app from inside of a startup or shutdown handler.

@uSpike
Copy link
Contributor Author

uSpike commented Dec 19, 2021

Hi, sorry I've been out of the loop for a bit. I've rebased and cherry-picked @meshantz commit f5f1bfc into this PR.

@uSpike
Copy link
Contributor Author

uSpike commented Dec 19, 2021

I've fixed typing, formatting, and updated the docs to communicate that lifespan should be an AsyncContextManager. Should be ready for review now.

@mortbauer
Copy link

@tiangolo this seems like a straight forward PR, please have a look if you can, would be great!!

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2023

📝 Docs preview for commit 5ae6547 at: https://64062412c1f3f7106eff7bd8--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2023

📝 Docs preview for commit 8b6dd1a at: https://640626d9f5524310c7db8154--fastapi.netlify.app

@tiangolo tiangolo changed the title Add lifespan context option ✨ Add support for lifespan async context managers (superseding startup and shutdown events) Mar 7, 2023
@tiangolo tiangolo added feature New feature or request and removed investigate labels Mar 7, 2023
@tiangolo
Copy link
Owner

tiangolo commented Mar 7, 2023

Thank you @uSpike! 🙇 🍰

I updated the docs to include more info and make this the recommended way from now on.

I also took the updates in #5503, thanks @JonathanPlasse ! ☕

And I added tests for the new example in the docs.

This will be available in FastAPI 0.93.0, released in the next few hours. 🎉

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo merged commit cc9a73c into tiangolo:master Mar 7, 2023
@uSpike uSpike deleted the add-lifespan-context branch March 7, 2023 16:39
mahenzon pushed a commit to mahenzon/fastapi that referenced this pull request Mar 11, 2023
…rtup` and `shutdown` events) (tiangolo#2944)

Co-authored-by: Mike Shantz <mshantz@coldstorage.com>
Co-authored-by: Jonathan Plasse <13716151+JonathanPlasse@users.noreply.github.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support starlette "lifespan" context for application
9 participants