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.

AsyncIOScheduler won't run if it's initialized before running uvicorn

See original GitHub issue

Describe the bug The test job does not run

To Reproduce

from os import environ
from apscheduler.executors.asyncio import AsyncIOExecutor
from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.jobstores.memory import MemoryJobStore
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pytz import utc

"""
    Local async function scheduler allows periodic or
    timed function execution. Uses Redis backend to queue
    up jobs so multiple workers and coordinate jobs.
"""

job_store = (
    MemoryJobStore()
)  # FIXME due to a bug in the apscheduler + gunicorn combination


scheduler: AsyncIOScheduler = AsyncIOScheduler(
    jobstores={
        "default": job_store,
    },
    executors={"default": AsyncIOExecutor(), "cron": ThreadPoolExecutor()},
    # timezone=utc,
    job_defaults={
        "coalesce": True,  # Trigger only one job to make up for missed jobs.
        "max_instances": 1,  # Allow only one execution of a job per time.
    },
)

if __name__ == "__main__":
    from time import sleep

    @scheduler.scheduled_job(
        'cron',
        day_of_week='mon-fri',
        hour='*',
        minute='0-59',
        timezone="US/Pacific",
    )
    def test_job():
        print("test job ran")

    scheduler.start()

    while 1:
        print("...")
        sleep(4)

Expected behavior test job ran should be printed.

Additional context None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
imirzadehcommented, Aug 5, 2021

Yeah, that was my problem too. But since I’m using FastAPI and I use the AsyncScheduler from the API, I can run:

uvicorn server:app --host <HOST> --port <PORT> --reload

and it works fine for me.

1reaction
agronholmcommented, Jan 16, 2021

You are required to run the code that schedules the jobs. The decorator is just an alternate way to do schedule.add_job() after all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make sure only one worker launches the apscheduler event in ...
The --preload flag tells Gunicorn to "load the app before forking the ... If they are run by different processes, the coordination wouldn't ......
Read more >
User guide — APScheduler 3.9.1 documentation
Each job has its own trigger which determines when the job should be run next. Beyond their initial configuration, triggers are completely stateless....
Read more >
python-quart/lobby - Gitter
I was wondering if my understanding of the Flask-SQLAlchemy issue is correct: Flask-SQLAlchemy (and SQLAlchemy's ORM) runs in a single thread. If it's...
Read more >
apscheduler - Reddit post and comment search - SocialGrep
I am using APScheduler to create scheduled tasks in Django. My scheduled task updates all instances of a particular model, so the script...
Read more >
APScheduler Documentation - Read the Docs
recreate your jobs at the start of your application, ... Initialize the rest of the application here, or before the scheduler initialization.
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