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

[✨Update✨] How to use with Latest FastAPI version #15

Merged
merged 2 commits into from
Feb 29, 2024

Conversation

priyanshu-panwar
Copy link
Owner

[✨Update✨] How to use with Latest FastAPI version

With the latest FastAPI version, on_event lifespan functions are depreceated. Here is the official doc.
We need to make use of asynccontextmanager with the latest fastapi.

Here is an example how to use lifespan (Repeated Tasks) functions with latest fastapi:

from fastapi import FastAPI
from contextlib import asynccontextmanager
from fastapi_utilities.repeat import repeat_every, repeat_at

@asynccontextmanager
async def lifespan(app: FastAPI):
    # --- startup ---
    await test()
    test2()
    yield
    # --- shutdown ---

app = FastAPI(lifespan=lifespan)

# Repeat Every Example
@repeat_every(seconds=2)
async def test():
    print("test")

# Repeat At Example
@repeat_at(cron="* * * * *")
def test2():
    print("test2")

Only difference is to call our tasks from lifespan function instead of using on_event function.

@priyanshu-panwar priyanshu-panwar linked an issue Feb 28, 2024 that may be closed by this pull request
Copy link

codecov bot commented Feb 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.92%. Comparing base (a427dfe) to head (d27cafd).

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #15   +/-   ##
=======================================
  Coverage   75.92%   75.92%           
=======================================
  Files           9        9           
  Lines         162      162           
=======================================
  Hits          123      123           
  Misses         39       39           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@vorenhoutgithub vorenhoutgithub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add the link to the correct way. Instead of the deprecated, add link for https://fastapi.tiangolo.com/advanced/events/#async-context-manager

@priyanshu-panwar priyanshu-panwar merged commit e02d4db into master Feb 29, 2024
9 checks passed
@priyanshu-panwar priyanshu-panwar deleted the 13-on_eventstartup-is-deprecated branch February 29, 2024 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.on_event('startup') is deprecated
2 participants