From 42941673a50cc235d25daf0da2cf1cca4255004b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Sat, 22 Feb 2014 09:18:58 +0200 Subject: Implemented integration with various event loops --- examples/interval.py | 21 --------------------- examples/persistent.py | 7 ++++--- examples/reference.py | 5 +++-- examples/schedulers/asyncio_.py | 25 +++++++++++++++++++++++++ examples/schedulers/background.py | 26 ++++++++++++++++++++++++++ examples/schedulers/blocking.py | 22 ++++++++++++++++++++++ examples/schedulers/gevent_.py | 24 ++++++++++++++++++++++++ examples/schedulers/qt.py | 37 +++++++++++++++++++++++++++++++++++++ examples/schedulers/tornado_.py | 25 +++++++++++++++++++++++++ examples/schedulers/twisted_.py | 25 +++++++++++++++++++++++++ examples/threaded.py | 25 ------------------------- 11 files changed, 191 insertions(+), 51 deletions(-) delete mode 100644 examples/interval.py create mode 100644 examples/schedulers/asyncio_.py create mode 100644 examples/schedulers/background.py create mode 100644 examples/schedulers/blocking.py create mode 100644 examples/schedulers/gevent_.py create mode 100644 examples/schedulers/qt.py create mode 100644 examples/schedulers/tornado_.py create mode 100644 examples/schedulers/twisted_.py delete mode 100644 examples/threaded.py (limited to 'examples') diff --git a/examples/interval.py b/examples/interval.py deleted file mode 100644 index 5469fef..0000000 --- a/examples/interval.py +++ /dev/null @@ -1,21 +0,0 @@ -""" -Basic example showing how to start the scheduler and schedule a job that executes on 3 second intervals. -""" - -from datetime import datetime - -from apscheduler.scheduler import Scheduler - - -def tick(): - print('Tick! The time is: %s' % datetime.now()) - - -if __name__ == '__main__': - scheduler = Scheduler(standalone=True) - scheduler.add_job(tick, 'interval', {'seconds': 3}) - print('Press Ctrl+C to exit') - try: - scheduler.start() - except (KeyboardInterrupt, SystemExit): - pass 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/schedulers/blocking.py b/examples/schedulers/blocking.py new file mode 100644 index 0000000..55d1385 --- /dev/null +++ b/examples/schedulers/blocking.py @@ -0,0 +1,22 @@ +""" +Demonstrates how to use the blocking scheduler to schedule a job that executes on 3 second intervals. +""" + +from datetime import datetime + +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+C to exit') + + try: + scheduler.start() + except (KeyboardInterrupt, SystemExit): + pass 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) -- cgit v1.2.1