from apscheduler.schedulers.asyncio import AsyncIOScheduler async def task1(): print("Task 1 executed") async def task2(): print("Task 2 executed") # Create an AsyncIOScheduler instance scheduler = AsyncIOScheduler() # Schedule task1 to run every 5 seconds and task2 every 10 seconds scheduler.add_job(task1, 'interval', seconds=5) scheduler.add_job(task2, 'interval', seconds=10) # Start the scheduler scheduler.start()
from asyncio import sleep from apscheduler.schedulers.asyncio import AsyncIOScheduler async def coroutine_task(): await sleep(5) print("Coroutine task executed") scheduler = AsyncIOScheduler() # Schedule the coroutine_task to run once using the cron trigger scheduler.add_job(coroutine_task, 'cron', minute=0) # Start the scheduler scheduler.start()Here, the coroutine_task runs once at the start of every hour, using the cron trigger. The task performs an asynchronous sleep for 5 seconds before printing a message. The AsyncIOScheduler instance is then started to initiate the scheduled task. In conclusion, the AsyncIOScheduler is a general-purpose task scheduler built for Python's asyncio library. It is available as a part of the APScheduler library, which provides scheduling options for various use cases. It is well-suited for applications that need to run a large number of scheduled asyncio tasks with high precision and low overhead.