diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2012-09-04 09:26:54 +0300 |
---|---|---|
committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2012-09-04 09:26:54 +0300 |
commit | e1614de75f767482787512b7ea172138e6d5038d (patch) | |
tree | 6ac319ad6c2e1195deefe3a388f8e9cc8d82d46f /examples | |
parent | 068d4eb40c18b9d0e6da9110f90fe679eba6e964 (diff) | |
download | apscheduler-e1614de75f767482787512b7ea172138e6d5038d.tar.gz |
Added a standalone operating mode
Diffstat (limited to 'examples')
-rw-r--r-- | examples/interval.py | 21 | ||||
-rw-r--r-- | examples/persistent.py | 30 | ||||
-rw-r--r-- | examples/threaded.py | 26 |
3 files changed, 45 insertions, 32 deletions
diff --git a/examples/interval.py b/examples/interval.py index e449c15..e16596c 100644 --- a/examples/interval.py +++ b/examples/interval.py @@ -1,27 +1,22 @@ """ Basic example showing how to start the scheduler and schedule a job that -executes on 3 second intervals. It uses sys.stdout.write instead of print to -allow it to work unmodified on both Python 2.x and 3.x. +executes on 3 second intervals. """ from datetime import datetime -import sys -import time from apscheduler.scheduler import Scheduler def tick(): - sys.stdout.write('Tick! The time is: %s\n' % datetime.now()) + print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__': - scheduler = Scheduler() + scheduler = Scheduler(standalone=True) scheduler.add_interval_job(tick, seconds=3) - sys.stdout.write('Press Ctrl+C to exit\n') - scheduler.start() - - # This is here to prevent the main thread from exiting so that the - # scheduler has time to work -- this should not be necessary in real world - # applications - time.sleep(9999) + print('Press Ctrl+C to exit') + try: + scheduler.start() + except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/persistent.py b/examples/persistent.py index 292054e..4233e3c 100644 --- a/examples/persistent.py +++ b/examples/persistent.py @@ -1,37 +1,29 @@ """ This example demonstrates the use of persistent job stores. On each run, it -adds a new alarm that fires after one minute. You can exit the program, restart -it and observe that any previous alarms that have not fired yet are still -active. +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. """ from datetime import datetime, timedelta -import sys -import time from apscheduler.scheduler import Scheduler from apscheduler.jobstores.shelve_store import ShelveJobStore def alarm(time): - sys.stdout.write('Alarm! This alarm was scheduled at %s.\n' % time) + print('Alarm! This alarm was scheduled at %s.' % time) if __name__ == '__main__': - scheduler = Scheduler() + scheduler = Scheduler(standalone=True) scheduler.add_jobstore(ShelveJobStore('example.db'), 'shelve') - alarm_time = datetime.now() + timedelta(minutes=1) + alarm_time = datetime.now() + timedelta(seconds=10) scheduler.add_date_job(alarm, alarm_time, name='alarm', jobstore='shelve', args=[datetime.now()]) - sys.stdout.write('To clear the alarms, delete the example.db file.\n') - sys.stdout.write('Press Ctrl+C to exit\n') - scheduler.start() - + print('To clear the alarms, delete the example.db file.') + print('Press Ctrl+C to exit') try: - # This is here to prevent the main thread from exiting so that the - # scheduler has time to work -- this is rarely necessary in real world - # applications - time.sleep(9999) - finally: - # Shut down the scheduler so that the job store gets closed properly - scheduler.shutdown() + scheduler.start() + except (KeyboardInterrupt, SystemExit): + pass diff --git a/examples/threaded.py b/examples/threaded.py new file mode 100644 index 0000000..b80f36f --- /dev/null +++ b/examples/threaded.py @@ -0,0 +1,26 @@ +""" +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_interval_job(tick, 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) |