diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2014-02-22 09:18:58 +0200 |
---|---|---|
committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2014-02-24 08:58:11 +0200 |
commit | 42941673a50cc235d25daf0da2cf1cca4255004b (patch) | |
tree | e24d8ec8f30b888b3288c7fea534455bed858e13 /examples | |
parent | 6e6649ad14bf3397f0be3a22676968f006969c5f (diff) | |
download | apscheduler-42941673a50cc235d25daf0da2cf1cca4255004b.tar.gz |
Implemented integration with various event loops
Diffstat (limited to 'examples')
-rw-r--r-- | examples/persistent.py | 7 | ||||
-rw-r--r-- | examples/reference.py | 5 | ||||
-rw-r--r-- | examples/schedulers/asyncio_.py | 25 | ||||
-rw-r--r-- | examples/schedulers/background.py | 26 | ||||
-rw-r--r-- | examples/schedulers/blocking.py (renamed from examples/interval.py) | 7 | ||||
-rw-r--r-- | examples/schedulers/gevent_.py | 24 | ||||
-rw-r--r-- | examples/schedulers/qt.py | 37 | ||||
-rw-r--r-- | examples/schedulers/tornado_.py | 25 | ||||
-rw-r--r-- | examples/schedulers/twisted_.py | 25 | ||||
-rw-r--r-- | examples/threaded.py | 25 |
10 files changed, 173 insertions, 33 deletions
diff --git a/examples/persistent.py b/examples/persistent.py index b75b778..1e1a155 100644 --- a/examples/persistent.py +++ b/examples/persistent.py @@ -6,8 +6,8 @@ You can exit the program, restart it and observe that any previous alarms that h from datetime import datetime, timedelta -from apscheduler.scheduler import Scheduler -from apscheduler.jobstores.shelve_store import ShelveJobStore +from apscheduler.schedulers.blocking import BlockingScheduler +from apscheduler.jobstores.shelve import ShelveJobStore def alarm(time): @@ -15,12 +15,13 @@ def alarm(time): if __name__ == '__main__': - scheduler = Scheduler(standalone=True) + scheduler = BlockingScheduler() scheduler.add_jobstore(ShelveJobStore('example.db'), 'shelve') alarm_time = datetime.now() + timedelta(seconds=10) scheduler.add_job(alarm, 'simple', [alarm_time], jobstore='shelve', args=[datetime.now()]) print('To clear the alarms, delete the example.db file.') print('Press Ctrl+C to exit') + try: scheduler.start() except (KeyboardInterrupt, SystemExit): diff --git a/examples/reference.py b/examples/reference.py index dc9756c..2b0c414 100644 --- a/examples/reference.py +++ b/examples/reference.py @@ -2,13 +2,14 @@ Basic example showing how to schedule a callable using a textual reference. """ -from apscheduler.scheduler import Scheduler +from apscheduler.schedulers.blocking import BlockingScheduler if __name__ == '__main__': - scheduler = Scheduler(standalone=True) + scheduler = BlockingScheduler() scheduler.add_job('sys:stdout.write', 'interval', {'seconds': 3}, args=['tick\n']) print('Press Ctrl+C to exit') + try: scheduler.start() except (KeyboardInterrupt, SystemExit): diff --git a/examples/schedulers/asyncio_.py b/examples/schedulers/asyncio_.py new file mode 100644 index 0000000..01cc138 --- /dev/null +++ b/examples/schedulers/asyncio_.py @@ -0,0 +1,25 @@ +""" +Demonstrates how to use the Tornado compatible scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime +import asyncio + +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+C to exit') + + # Execution will block here until Ctrl+C 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 new file mode 100644 index 0000000..8a3dde5 --- /dev/null +++ b/examples/schedulers/background.py @@ -0,0 +1,26 @@ +""" +Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime +import time + +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+C to exit') + + try: + # This is here to simulate application activity (which keeps the main thread alive). + while True: + time.sleep(2) + except (KeyboardInterrupt, SystemExit): + scheduler.shutdown() # Not strictly necessary if daemonic mode is enabled but should be done if possible diff --git a/examples/interval.py b/examples/schedulers/blocking.py index 5469fef..55d1385 100644 --- a/examples/interval.py +++ b/examples/schedulers/blocking.py @@ -1,10 +1,10 @@ """ -Basic example showing how to start the scheduler and schedule a job that executes on 3 second intervals. +Demonstrates how to use the blocking scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime -from apscheduler.scheduler import Scheduler +from apscheduler.schedulers.blocking import BlockingScheduler def tick(): @@ -12,9 +12,10 @@ def tick(): if __name__ == '__main__': - scheduler = Scheduler(standalone=True) + scheduler = BlockingScheduler() scheduler.add_job(tick, 'interval', {'seconds': 3}) print('Press Ctrl+C to exit') + try: scheduler.start() except (KeyboardInterrupt, SystemExit): diff --git a/examples/schedulers/gevent_.py b/examples/schedulers/gevent_.py new file mode 100644 index 0000000..597aa4d --- /dev/null +++ b/examples/schedulers/gevent_.py @@ -0,0 +1,24 @@ +""" +Demonstrates how to use the gevent compatible scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime + +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+C to exit') + + # Execution will block here until Ctrl+C is pressed. + try: + g.join() + except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/schedulers/qt.py b/examples/schedulers/qt.py new file mode 100644 index 0000000..15b9192 --- /dev/null +++ b/examples/schedulers/qt.py @@ -0,0 +1,37 @@ +""" +Demonstrates how to use the Qt compatible scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime +import signal +import sys + +from apscheduler.schedulers.qt import QtScheduler + +try: + from PyQt5.QtWidgets import QApplication, QLabel +except ImportError: + try: + from PyQt4.QtGui import QApplication, QLabel + except ImportError: + from PySide.QtGui import QApplication, QLabel + + +def tick(): + label.setText('Tick! The time is: %s' % datetime.now()) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + signal.signal(signal.SIGINT, lambda *args: QApplication.quit()) # This enables processing of Ctrl+C keypresses + label = QLabel('The timer text will appear here in a moment!') + label.setWindowTitle('QtScheduler example') + label.setFixedSize(280, 50) + label.show() + + scheduler = QtScheduler() + scheduler.add_job(tick, 'interval', {'seconds': 3}) + scheduler.start() + + # Execution will block here until the user closes the windows or Ctrl+C is pressed. + app.exec_() diff --git a/examples/schedulers/tornado_.py b/examples/schedulers/tornado_.py new file mode 100644 index 0000000..42ddab5 --- /dev/null +++ b/examples/schedulers/tornado_.py @@ -0,0 +1,25 @@ +""" +Demonstrates how to use the Tornado compatible scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime + +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+C to exit') + + # Execution will block here until Ctrl+C is pressed. + try: + IOLoop.instance().start() + except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/schedulers/twisted_.py b/examples/schedulers/twisted_.py new file mode 100644 index 0000000..a1f7358 --- /dev/null +++ b/examples/schedulers/twisted_.py @@ -0,0 +1,25 @@ +""" +Demonstrates how to use the Twisted compatible scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime + +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+C to exit') + + # Execution will block here until Ctrl+C is pressed. + try: + reactor.run() + except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/threaded.py b/examples/threaded.py deleted file mode 100644 index 7a11937..0000000 --- a/examples/threaded.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -Basic example showing how the scheduler integrates with the application it's running alongside with. -""" - -from datetime import datetime -import time - -from apscheduler.scheduler import Scheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = Scheduler() - scheduler.add_job(tick, 'interval', {'seconds': 3}) - print('Press Ctrl+C to exit') - scheduler.start() - - # This is here to simulate application activity (which keeps the main - # thread alive). - while True: - print('This is the main thread.') - time.sleep(2) |