question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] uvicorn can't reload when i use apscheduler and fastapi together

See original GitHub issue

when i use apscheduler and fastapi together like this:

def get_app():
    app = FastAPI()
    return app

def apscheduler():
    scheduler = AsyncIOScheduler()
    scheduler.add_job(my_jobs.tick, 'cron', hour=2, minute=0)
    deadline = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta(seconds=5)
    scheduler.add_job(my_jobs.tick, 'date', run_date=deadline)
    try:
        scheduler.start()
    except Exception as e:
        scheduler.shutdown()

apscheduler()
app = get_app()

uvicorn can’t reload automaticly like before here is my start command:

uvicorn main:app --reload

Thank you very much if anyone can help.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
selimbcommented, Aug 3, 2022

@anne17 That’s a perfect use of app.state 😃 IMO this is a better practice than initializing everything at import time in the module scope, as recommended in the docs.

0reactions
anne17commented, Aug 3, 2022

For me it worked to shut down the scheduler in the shutdown event as suggested by @selimb. I am not sure at all if this is best practice, but I store the scheduler in app.state in the startup event and then I can access it later:

@app.on_event("startup")
async def start_scheduler():
    scheduler = AsyncIOScheduler(timezone="Europe/Stockholm")
    scheduler.add_job(...)
    scheduler.start()
    app.state.scheduler = scheduler


@app.on_event("shutdown")
async def stop_scheduler():
    app.state.scheduler.shutdown(wait=False)

Now uvicorn’s --reload works as it should.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI reload stops during reload - gunicorn - Stack Overflow
and the API seems to not be changing when it runs, still operating in the state the server started.
Read more >
uvicorn - Reddit post and comment search - SocialGrep
I have developed a fastAPI application which is served via gunicorn with uvicorn workers. We noticed that APScheduler (AsycIOScheduler) is working with uvicorn...
Read more >
FastAPI in Containers - Docker
Build a Docker Image with a Single-File FastAPI​​ Then adjust the Uvicorn command to use the new module main instead of app. main...
Read more >
Software Packages in "jammy", Subsection python - Ubuntu
Construct, convert, and manipulate colors in a Pythonic manner. python3-colour (0.1.5-2) [universe]: converts and manipulates various color representation - ...
Read more >
Browse Python Technical Problem Clusters - Code Grepper
warning: there was an error checking the latest version of pip. conda install fastapi · modulenotfounderror: no module named 'png' · iterate over...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found