summaryrefslogtreecommitdiff
path: root/examples/schedulers/background.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/schedulers/background.py')
-rw-r--r--examples/schedulers/background.py26
1 files changed, 26 insertions, 0 deletions
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