Propagate errors to main asyncio loop #788
-
I have a program which runs asyncio loop, starts tasks and at the end runs AsyncIOScheduler jobs with
But if I catch any exception in one of AsyncIOScheduler tasks it just prints the error message in logs and does not call How can I propagate AsyncIOScheduler to the main asyncio loop and run For example this job will print the error and wont stop loop:
And lines in logs:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
What you can do is add an event listener to the scheduler to react to job errors, and have that set the exception on a future that your application awaits on: import asyncio
from apscheduler.events import EVENT_JOB_ERROR
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.date import DateTrigger
def raise_error():
raise RuntimeError
async def main():
future = asyncio.get_running_loop().create_future()
scheduler = AsyncIOScheduler()
scheduler.add_listener(lambda event: future.set_exception(event.exception), EVENT_JOB_ERROR)
scheduler.add_job(raise_error, DateTrigger())
scheduler.start()
await future
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
What you can do is add an event listener to the scheduler to react to job errors, and have that set the exception on a future that your application awaits on: