AsyncIOScheduler won't run if it's initialized before running uvicorn
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, that was my problem too. But since I’m using FastAPI and I use the AsyncScheduler from the API, I can run:
and it works fine for me.
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.