diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2020-09-20 14:30:46 +0300 |
---|---|---|
committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2020-09-20 17:00:42 +0300 |
commit | 95169d277da6501b181a956791e7ea0171fbae64 (patch) | |
tree | f9ea601ff84506ff8d44799e6cdcfa723346aed9 /examples | |
parent | 6f6b36d83369cfb95b6b0071caf82c89818ef020 (diff) | |
download | apscheduler-95169d277da6501b181a956791e7ea0171fbae64.tar.gz |
Added the first usable scheduler, worker and datastore implementations
Diffstat (limited to 'examples')
-rw-r--r-- | examples/jobstores/mongodb.py | 33 | ||||
-rw-r--r-- | examples/jobstores/redis_.py | 33 | ||||
-rw-r--r-- | examples/jobstores/rethinkdb_.py | 33 | ||||
-rw-r--r-- | examples/jobstores/sqlalchemy_.py | 32 | ||||
-rw-r--r-- | examples/jobstores/zookeeper.py | 33 | ||||
-rw-r--r-- | examples/schedulers/async_.py | 20 | ||||
-rw-r--r-- | examples/schedulers/asyncio_.py | 27 | ||||
-rw-r--r-- | examples/schedulers/background.py | 29 | ||||
-rw-r--r-- | examples/schedulers/blocking.py | 24 | ||||
-rw-r--r-- | examples/schedulers/gevent_.py | 26 | ||||
-rw-r--r-- | examples/schedulers/sync.py | 16 | ||||
-rw-r--r-- | examples/schedulers/tornado_.py | 27 | ||||
-rw-r--r-- | examples/schedulers/twisted_.py | 27 |
13 files changed, 36 insertions, 324 deletions
diff --git a/examples/jobstores/mongodb.py b/examples/jobstores/mongodb.py deleted file mode 100644 index 22ee6c7..0000000 --- a/examples/jobstores/mongodb.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -This example demonstrates the use of the MongoDB job store. -On each run, it adds a new alarm that fires after ten seconds. -You can exit the program, restart it and observe that any previous alarms that have not fired yet -are still active. Running the example with the --clear switch will remove any existing alarms. -""" - -from datetime import datetime, timedelta -import sys -import os - -from apscheduler.schedulers.blocking import BlockingScheduler - - -def alarm(time): - print('Alarm! This alarm was scheduled at %s.' % time) - - -if __name__ == '__main__': - scheduler = BlockingScheduler() - scheduler.add_jobstore('mongodb', collection='example_jobs') - if len(sys.argv) > 1 and sys.argv[1] == '--clear': - scheduler.remove_all_jobs() - - alarm_time = datetime.now() + timedelta(seconds=10) - scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()]) - print('To clear the alarms, run this example with the --clear argument.') - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/jobstores/redis_.py b/examples/jobstores/redis_.py deleted file mode 100644 index 0f24183..0000000 --- a/examples/jobstores/redis_.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -This example demonstrates the use of the Redis job store. -On each run, it adds a new alarm that fires after ten seconds. -You can exit the program, restart it and observe that any previous alarms that have not fired yet -are still active. Running the example with the --clear switch will remove any existing alarms. -""" - -from datetime import datetime, timedelta -import sys -import os - -from apscheduler.schedulers.blocking import BlockingScheduler - - -def alarm(time): - print('Alarm! This alarm was scheduled at %s.' % time) - - -if __name__ == '__main__': - scheduler = BlockingScheduler() - scheduler.add_jobstore('redis', jobs_key='example.jobs', run_times_key='example.run_times') - if len(sys.argv) > 1 and sys.argv[1] == '--clear': - scheduler.remove_all_jobs() - - alarm_time = datetime.now() + timedelta(seconds=10) - scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()]) - print('To clear the alarms, run this example with the --clear argument.') - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/jobstores/rethinkdb_.py b/examples/jobstores/rethinkdb_.py deleted file mode 100644 index c1baa0e..0000000 --- a/examples/jobstores/rethinkdb_.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -This example demonstrates the use of the RethinkDB job store. -On each run, it adds a new alarm that fires after ten seconds. -You can exit the program, restart it and observe that any previous alarms that have not fired yet -are still active. Running the example with the --clear switch will remove any existing alarms. -""" - -from datetime import datetime, timedelta -import sys -import os - -from apscheduler.schedulers.blocking import BlockingScheduler - - -def alarm(time): - print('Alarm! This alarm was scheduled at %s.' % time) - - -if __name__ == '__main__': - scheduler = BlockingScheduler() - scheduler.add_jobstore('rethinkdb', database='apscheduler_example') - if len(sys.argv) > 1 and sys.argv[1] == '--clear': - scheduler.remove_all_jobs() - - alarm_time = datetime.now() + timedelta(seconds=10) - scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()]) - print('To clear the alarms, run this example with the --clear argument.') - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/jobstores/sqlalchemy_.py b/examples/jobstores/sqlalchemy_.py deleted file mode 100644 index 83e987b..0000000 --- a/examples/jobstores/sqlalchemy_.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -This example demonstrates the use of the SQLAlchemy job store. -On each run, it adds a new alarm that fires after ten seconds. -You can exit the program, restart it and observe that any previous alarms that have not fired yet -are still active. You can also give it the database URL as an argument. -See the SQLAlchemy documentation on how to construct those. -""" - -from datetime import datetime, timedelta -import sys -import os - -from apscheduler.schedulers.blocking import BlockingScheduler - - -def alarm(time): - print('Alarm! This alarm was scheduled at %s.' % time) - - -if __name__ == '__main__': - scheduler = BlockingScheduler() - url = sys.argv[1] if len(sys.argv) > 1 else 'sqlite:///example.sqlite' - scheduler.add_jobstore('sqlalchemy', url=url) - alarm_time = datetime.now() + timedelta(seconds=10) - scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()]) - print('To clear the alarms, delete the example.sqlite file.') - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/jobstores/zookeeper.py b/examples/jobstores/zookeeper.py deleted file mode 100644 index 12b3e42..0000000 --- a/examples/jobstores/zookeeper.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -This example demonstrates the use of the Zookeeper job store. -On each run, it adds a new alarm that fires after ten seconds. -You can exit the program, restart it and observe that any previous alarms that have not fired yet -are still active. Running the example with the --clear switch will remove any existing alarms. -""" - -from datetime import datetime, timedelta -import sys -import os - -from apscheduler.schedulers.blocking import BlockingScheduler - - -def alarm(time): - print('Alarm! This alarm was scheduled at %s.' % time) - - -if __name__ == '__main__': - scheduler = BlockingScheduler() - scheduler.add_jobstore('zookeeper', path='/example_jobs') - if len(sys.argv) > 1 and sys.argv[1] == '--clear': - scheduler.remove_all_jobs() - - alarm_time = datetime.now() + timedelta(seconds=10) - scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()]) - print('To clear the alarms, run this example with the --clear argument.') - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/schedulers/async_.py b/examples/schedulers/async_.py new file mode 100644 index 0000000..0f6677c --- /dev/null +++ b/examples/schedulers/async_.py @@ -0,0 +1,20 @@ +import logging + +import anyio +from apscheduler.schedulers.async_ import AsyncScheduler +from apscheduler.triggers.interval import IntervalTrigger + + +def say_hello(): + print('Hello!') + + +async def main(): + async with AsyncScheduler() as scheduler: + await scheduler.add_schedule(say_hello, IntervalTrigger(seconds=1)) + +logging.basicConfig(level=logging.DEBUG) +try: + anyio.run(main) +except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/schedulers/asyncio_.py b/examples/schedulers/asyncio_.py deleted file mode 100644 index a438ae1..0000000 --- a/examples/schedulers/asyncio_.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -Demonstrates how to use the asyncio compatible scheduler to schedule a job that executes on 3 -second intervals. -""" - -import asyncio -import os -from datetime import datetime - -from apscheduler.schedulers.asyncio import AsyncIOScheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = AsyncIOScheduler() - scheduler.add_job(tick, 'interval', seconds=3) - scheduler.start() - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed. - try: - asyncio.get_event_loop().run_forever() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/schedulers/background.py b/examples/schedulers/background.py deleted file mode 100644 index 8ac1ae9..0000000 --- a/examples/schedulers/background.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -Demonstrates how to use the background scheduler to schedule a job that executes on 3 second -intervals. -""" - -from datetime import datetime -import time -import os - -from apscheduler.schedulers.background import BackgroundScheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = BackgroundScheduler() - scheduler.add_job(tick, 'interval', seconds=3) - scheduler.start() - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - # This is here to simulate application activity (which keeps the main thread alive). - while True: - time.sleep(2) - except (KeyboardInterrupt, SystemExit): - # Not strictly necessary if daemonic mode is enabled but should be done if possible - scheduler.shutdown() diff --git a/examples/schedulers/blocking.py b/examples/schedulers/blocking.py deleted file mode 100644 index 5b1ce3e..0000000 --- a/examples/schedulers/blocking.py +++ /dev/null @@ -1,24 +0,0 @@ -""" -Demonstrates how to use the blocking scheduler to schedule a job that executes on 3 second -intervals. -""" - -from datetime import datetime -import os - -from apscheduler.schedulers.blocking import BlockingScheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = BlockingScheduler() - scheduler.add_job(tick, 'interval', seconds=3) - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/schedulers/gevent_.py b/examples/schedulers/gevent_.py deleted file mode 100644 index 8b23402..0000000 --- a/examples/schedulers/gevent_.py +++ /dev/null @@ -1,26 +0,0 @@ -""" -Demonstrates how to use the gevent compatible scheduler to schedule a job that executes on 3 second -intervals. -""" - -from datetime import datetime -import os - -from apscheduler.schedulers.gevent import GeventScheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = GeventScheduler() - scheduler.add_job(tick, 'interval', seconds=3) - g = scheduler.start() # g is the greenlet that runs the scheduler loop - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed. - try: - g.join() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/schedulers/sync.py b/examples/schedulers/sync.py new file mode 100644 index 0000000..df78df9 --- /dev/null +++ b/examples/schedulers/sync.py @@ -0,0 +1,16 @@ +import logging + +from apscheduler.schedulers.sync import SyncScheduler +from apscheduler.triggers.interval import IntervalTrigger + + +def say_hello(): + print('Hello!') + + +logging.basicConfig(level=logging.DEBUG) +try: + with SyncScheduler() as scheduler: + scheduler.add_schedule(say_hello, IntervalTrigger(seconds=1)) +except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/schedulers/tornado_.py b/examples/schedulers/tornado_.py deleted file mode 100644 index fe689af..0000000 --- a/examples/schedulers/tornado_.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -Demonstrates how to use the Tornado compatible scheduler to schedule a job that executes on 3 -second intervals. -""" - -from datetime import datetime -import os - -from tornado.ioloop import IOLoop -from apscheduler.schedulers.tornado import TornadoScheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = TornadoScheduler() - scheduler.add_job(tick, 'interval', seconds=3) - scheduler.start() - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed. - try: - IOLoop.instance().start() - except (KeyboardInterrupt, SystemExit): - pass diff --git a/examples/schedulers/twisted_.py b/examples/schedulers/twisted_.py deleted file mode 100644 index 46413cb..0000000 --- a/examples/schedulers/twisted_.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -Demonstrates how to use the Twisted compatible scheduler to schedule a job that executes on 3 -second intervals. -""" - -from datetime import datetime -import os - -from twisted.internet import reactor -from apscheduler.schedulers.twisted import TwistedScheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = TwistedScheduler() - scheduler.add_job(tick, 'interval', seconds=3) - scheduler.start() - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) - - # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed. - try: - reactor.run() - except (KeyboardInterrupt, SystemExit): - pass |