diff options
Diffstat (limited to 'examples/separate_worker/async_scheduler.py')
-rw-r--r-- | examples/separate_worker/async_scheduler.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/separate_worker/async_scheduler.py b/examples/separate_worker/async_scheduler.py new file mode 100644 index 0000000..27eb37a --- /dev/null +++ b/examples/separate_worker/async_scheduler.py @@ -0,0 +1,47 @@ +""" +Example demonstrating the separation of scheduler and worker. +This script runs the scheduler part. You need to be running both this and the worker +script simultaneously in order for the scheduled task to be run. + +Requires the "postgresql" service to be running. +To install prerequisites: pip install sqlalchemy asyncpg +To run: python async_scheduler.py + +When run together with async_worker.py, it should print a line on the console +on a one-second interval. +""" + +from __future__ import annotations + +import asyncio +import logging + +from example_tasks import tick +from sqlalchemy.ext.asyncio import create_async_engine + +from apscheduler.datastores.async_sqlalchemy import AsyncSQLAlchemyDataStore +from apscheduler.eventbrokers.asyncpg import AsyncpgEventBroker +from apscheduler.schedulers.async_ import AsyncScheduler +from apscheduler.triggers.interval import IntervalTrigger + + +async def main(): + engine = create_async_engine( + "postgresql+asyncpg://postgres:secret@localhost/testdb" + ) + data_store = AsyncSQLAlchemyDataStore(engine) + event_broker = AsyncpgEventBroker.from_async_sqla_engine(engine) + + # Uncomment the next two lines to use the Redis event broker instead + # from apscheduler.eventbrokers.async_redis import AsyncRedisEventBroker + # event_broker = AsyncRedisEventBroker.from_url("redis://localhost") + + async with AsyncScheduler( + data_store, event_broker, start_worker=False + ) as scheduler: + await scheduler.add_schedule(tick, IntervalTrigger(seconds=1), id="tick") + await scheduler.wait_until_stopped() + + +logging.basicConfig(level=logging.INFO) +asyncio.run(main()) |