diff options
Diffstat (limited to 'examples/separate_worker/sync_scheduler.py')
-rw-r--r-- | examples/separate_worker/sync_scheduler.py | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/examples/separate_worker/sync_scheduler.py b/examples/separate_worker/sync_scheduler.py index 45d337d..4699018 100644 --- a/examples/separate_worker/sync_scheduler.py +++ b/examples/separate_worker/sync_scheduler.py @@ -1,14 +1,12 @@ """ -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. +This is an example demonstrating the use of the scheduler as only an interface to the +scheduling system. This script adds or updates a single schedule and then exits. To see +the schedule acted on, you need to run the corresponding worker script (either +async_worker.py or sync_worker.py). -Requires the "postgresql" and "redis" services to be running. -To install prerequisites: pip install sqlalchemy psycopg2 redis +This script requires the "postgresql" service to be running. +To install prerequisites: pip install sqlalchemy asyncpg To run: python sync_scheduler.py - -When run together with sync_worker.py, it should print a line on the console -on a one-second interval. """ from __future__ import annotations @@ -16,23 +14,22 @@ from __future__ import annotations import logging from example_tasks import tick -from sqlalchemy.future import create_engine +from sqlalchemy.ext.asyncio import create_async_engine -from apscheduler import SchedulerRole from apscheduler.datastores.sqlalchemy import SQLAlchemyDataStore -from apscheduler.eventbrokers.redis import RedisEventBroker +from apscheduler.eventbrokers.asyncpg import AsyncpgEventBroker from apscheduler.schedulers.sync import Scheduler from apscheduler.triggers.interval import IntervalTrigger logging.basicConfig(level=logging.INFO) -engine = create_engine("postgresql+psycopg2://postgres:secret@localhost/testdb") +engine = create_async_engine("postgresql+asyncpg://postgres:secret@localhost/testdb") data_store = SQLAlchemyDataStore(engine) -event_broker = RedisEventBroker.from_url("redis://localhost") +event_broker = AsyncpgEventBroker.from_async_sqla_engine(engine) # Uncomment the next two lines to use the MQTT event broker instead # from apscheduler.eventbrokers.mqtt import MQTTEventBroker # event_broker = MQTTEventBroker() -with Scheduler(data_store, event_broker, role=SchedulerRole.scheduler) as scheduler: +with Scheduler(data_store, event_broker) as scheduler: scheduler.add_schedule(tick, IntervalTrigger(seconds=1), id="tick") - scheduler.run_until_stopped() + # Note: we don't actually start the scheduler here! |