Skip to content

Commit f529a91

Browse files
committed
Add sched example code
1 parent 52231bb commit f529a91

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: source-code/sched/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Sched
2+
3+
Sometimes it can be useful to run functions at certain moments in time,
4+
similar to what cron does on Linux operatring systems.
5+
6+
## What is it?
7+
8+
* `cron.py`: example script using `schedule` that runs two tasks, one
9+
printing the time, and a computed value, the other updating the
10+
value to be printed. The first is scheduled every 2 seconds, the
11+
other every second.

Diff for: source-code/sched/cron.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
import schedule
4+
import time
5+
6+
7+
def print_event(event):
8+
time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
9+
print('{0}: {1}'.format(time_str, event['counter']))
10+
11+
12+
def set_event(event):
13+
event['counter'] += 1
14+
15+
if __name__ == '__main__':
16+
event = {'counter': 0}
17+
schedule.every(2).seconds.do(print_event, event)
18+
schedule.every().seconds.do(set_event, event)
19+
while True:
20+
schedule.run_pending()
21+
time.sleep(0.5)

0 commit comments

Comments
 (0)