File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments