diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2022-07-27 13:01:55 +0300 |
---|---|---|
committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2022-07-27 13:02:08 +0300 |
commit | e4a926f5ec113be711dad94773c45b34aebd0ff1 (patch) | |
tree | 8b9f1d9553080b06201d7d10ae43de440401cce2 /examples/standalone | |
parent | 0bfc8d54f358c0c26d613088547b1b025cd16a6e (diff) | |
download | apscheduler-e4a926f5ec113be711dad94773c45b34aebd0ff1.tar.gz |
Updated the examples for v4
Diffstat (limited to 'examples/standalone')
-rw-r--r-- | examples/standalone/async_memory.py | 28 | ||||
-rw-r--r-- | examples/standalone/async_mysql.py | 38 | ||||
-rw-r--r-- | examples/standalone/async_postgres.py | 38 | ||||
-rw-r--r-- | examples/standalone/sync_memory.py | 23 |
4 files changed, 127 insertions, 0 deletions
diff --git a/examples/standalone/async_memory.py b/examples/standalone/async_memory.py new file mode 100644 index 0000000..b3db775 --- /dev/null +++ b/examples/standalone/async_memory.py @@ -0,0 +1,28 @@ +""" +Example demonstrating use of the asynchronous scheduler in a simple asyncio app. + +To run: python async_memory.py + +It should print a line on the console on a one-second interval. +""" + +from __future__ import annotations + +from asyncio import run +from datetime import datetime + +from apscheduler.schedulers.async_ import AsyncScheduler +from apscheduler.triggers.interval import IntervalTrigger + + +def tick(): + print("Hello, the time is", datetime.now()) + + +async def main(): + async with AsyncScheduler() as scheduler: + await scheduler.add_schedule(tick, IntervalTrigger(seconds=1)) + await scheduler.wait_until_stopped() + + +run(main()) diff --git a/examples/standalone/async_mysql.py b/examples/standalone/async_mysql.py new file mode 100644 index 0000000..3f6f95f --- /dev/null +++ b/examples/standalone/async_mysql.py @@ -0,0 +1,38 @@ +""" +Example demonstrating use of the asynchronous scheduler with persistence via MySQL or +MariaDB in a simple asyncio app. + +Requires the "mysql" service to be running. +To install prerequisites: pip install sqlalchemy asyncmy +To run: python async_postgres.py + +It should print a line on the console on a one-second interval. +""" + +from __future__ import annotations + +from asyncio import run +from datetime import datetime + +from sqlalchemy.ext.asyncio import create_async_engine + +from apscheduler.datastores.async_sqlalchemy import AsyncSQLAlchemyDataStore +from apscheduler.schedulers.async_ import AsyncScheduler +from apscheduler.triggers.interval import IntervalTrigger + + +def tick(): + print("Hello, the time is", datetime.now()) + + +async def main(): + engine = create_async_engine( + "mysql+asyncmy://root:secret@localhost/testdb?charset=utf8mb4" + ) + data_store = AsyncSQLAlchemyDataStore(engine) + async with AsyncScheduler(data_store) as scheduler: + await scheduler.add_schedule(tick, IntervalTrigger(seconds=1), id="tick") + await scheduler.wait_until_stopped() + + +run(main()) diff --git a/examples/standalone/async_postgres.py b/examples/standalone/async_postgres.py new file mode 100644 index 0000000..e6083f7 --- /dev/null +++ b/examples/standalone/async_postgres.py @@ -0,0 +1,38 @@ +""" +Example demonstrating use of the asynchronous scheduler with persistence via PostgreSQL +in a simple asyncio app. + +Requires the "postgresql" service to be running. +To install prerequisites: pip install sqlalchemy asyncpg +To run: python async_postgres.py + +It should print a line on the console on a one-second interval. +""" + +from __future__ import annotations + +from asyncio import run +from datetime import datetime + +from sqlalchemy.ext.asyncio import create_async_engine + +from apscheduler.datastores.async_sqlalchemy import AsyncSQLAlchemyDataStore +from apscheduler.schedulers.async_ import AsyncScheduler +from apscheduler.triggers.interval import IntervalTrigger + + +def tick(): + print("Hello, the time is", datetime.now()) + + +async def main(): + engine = create_async_engine( + "postgresql+asyncpg://postgres:secret@localhost/testdb" + ) + data_store = AsyncSQLAlchemyDataStore(engine) + async with AsyncScheduler(data_store) as scheduler: + await scheduler.add_schedule(tick, IntervalTrigger(seconds=1), id="tick") + await scheduler.wait_until_stopped() + + +run(main()) diff --git a/examples/standalone/sync_memory.py b/examples/standalone/sync_memory.py new file mode 100644 index 0000000..6d804f7 --- /dev/null +++ b/examples/standalone/sync_memory.py @@ -0,0 +1,23 @@ +""" +Example demonstrating use of the synchronous scheduler. + +To run: python sync_memory.py + +It should print a line on the console on a one-second interval. +""" + +from __future__ import annotations + +from datetime import datetime + +from apscheduler.schedulers.sync import Scheduler +from apscheduler.triggers.interval import IntervalTrigger + + +def tick(): + print("Hello, the time is", datetime.now()) + + +with Scheduler() as scheduler: + scheduler.add_schedule(tick, IntervalTrigger(seconds=1)) + scheduler.wait_until_stopped() |