3
3
1 . [ Scheduling tasks] ( ./SCHEDULE.md#1-scheduling-tasks )
4
4
2 . [ Overview] ( ./SCHEDULE.md#2-overview )
5
5
3 . [ Installation] ( ./SCHEDULE.md#3-installation )
6
- 4 . [ The cron object] ( ./SCHEDULE.md#4-the-cron-object )
6
+ 4 . [ The cron object] ( ./SCHEDULE.md#4-the-cron-object ) How to specify times and dates
7
7
4.1 [ Time specifiers] ( ./SCHEDULE.md#41-time-specifiers )
8
8
4.2 [ The time to an event] ( ./SCHEDULE.md#42-the-time-to-an-event )
9
9
4.3 [ How it works] ( ./SCHEDULE.md#43-how-it-works )
10
10
4.4 [ Calendar behaviour] ( ./SCHEDULE.md#44-calendar-behaviour )
11
11
4.5 [ Limitations] ( ./SCHEDULE.md#45-limitations )
12
12
4.6 [ The Unix build] ( ./SCHEDULE.md#46-the-unix-build )
13
13
5 . [ The schedule function] ( ./SCHEDULE.md#5-the-schedule-function ) The primary interface for uasyncio
14
- 6 . [ Use in synchronous code ] ( ./SCHEDULE.md#6-use-in-synchronous-code ) If you really must
15
- 7 . [ Hardware timing limitations ] ( ./SCHEDULE.md#7-hardware-timing-limitations )
14
+ 6 . [ Hardware timing limitations ] ( ./SCHEDULE.md#6-hardware-timing-limitations )
15
+ 7 . [ Use in synchronous code ] ( ./SCHEDULE.md#7-use-in-synchronous-code ) If you really must
16
16
17
17
##### [ Tutorial] ( ./TUTORIAL.md#contents )
18
18
##### [ Main V3 README] ( ../README.md )
@@ -64,10 +64,24 @@ move to the parent directory of `sched` and issue:
64
64
```
65
65
Adapt the destination as appropriate for your hardware.
66
66
67
+ The following files are installed in the ` sched ` directory.
68
+ 1 . ` cron.py ` Computes time to next event.
69
+ 2 . ` sched.py ` The ` uasyncio ` ` schedule ` function: schedule a callback or coro.
70
+ 3 . ` primitives/__init__.py ` Necessary for ` sched.py ` .
71
+ 4 . ` asynctest.py ` Demo of asynchronous scheduling.
72
+ 5 . ` synctest.py ` Synchronous scheduling demo. For ` uasyncio ` phobics only.
73
+ 6 . ` crontest.py ` A test for ` cron.py ` code.
74
+ 7 . ` __init__.py ` Empty file for Python package.
75
+
76
+ The ` crontest ` script is only of interest to those wishing to adapt ` cron.py ` .
77
+ To run error-free a bare metal target should be used for the reason discussed
78
+ [ here] ( ./SCHEDULE.md#46-the-unix-build ) .
79
+
67
80
# 4. The cron object
68
81
69
82
This is a closure. It accepts a time specification for future events. Each call
70
- returns the number of seconds to wait for the next event to occur.
83
+ when passed the current time returns the number of seconds to wait for the next
84
+ event to occur.
71
85
72
86
It takes the following keyword-only args. A flexible set of data types are
73
87
accepted. These are known as ` Time specifiers ` and described below. Valid
@@ -148,7 +162,7 @@ the month.
148
162
149
163
Specifying ` wday=d ` and ` mday=n ` where n > 22 could result in a day beyond the
150
164
end of the month. It's not obvious what constitutes rational behaviour in this
151
- pathological corner case: a ` ValueError ` will result .
165
+ pathological corner case. Validation will throw a ` ValueError ` in this case .
152
166
153
167
### 4.4.2 Time causing month rollover
154
168
@@ -165,7 +179,7 @@ will change to the next valid month. This code, run at 9am on 31st July, would
165
179
aim to run the event at 1.59 on 1st October.
166
180
``` python
167
181
my_cron(month = (2 , 7 , 10 ), hrs = 1 , mins = 59 ) # moves forward 1 day
168
- t_wait = my_cron(time.time()) # but month may be disallowed
182
+ t_wait = my_cron(time.time()) # Next month is disallowed so jumps to October
169
183
```
170
184
171
185
##### [ Top] ( ./SCHEDULE.md#0-contents )
@@ -184,14 +198,14 @@ depends on the complexity of the time specifiers.
184
198
185
199
On hardware platforms the MicroPython ` time ` module does not handle daylight
186
200
saving time. Scheduled times are relative to system time. This does not apply
187
- to the Unix build.
201
+ to the Unix build where daylight saving needs to be considered .
188
202
189
203
## 4.6 The Unix build
190
204
191
- Asynchronous use requires ` uasyncio ` V3, so ensure this is installed on a Linux
192
- box .
205
+ Asynchronous use requires ` uasyncio ` V3, so ensure this is installed on the
206
+ Linux target .
193
207
194
- The synchronous and asynchronous demos run under the Unix build: it should be
208
+ The synchronous and asynchronous demos run under the Unix build. The module is
195
209
usable on Linux provided the daylight saving time (DST) constraints below are
196
210
met.
197
211
@@ -200,12 +214,10 @@ and duplicates when they go back. Scheduling those times will fail. A solution
200
214
is to avoid scheduling the times in your region where this occurs (01.00.00 to
201
215
02.00.00 in March and October here).
202
216
203
- The ` crontest.py ` test program produces failures under Unix. Most of these
204
- result from the fact that the Unix ` localtime ` function handles daylight saving
205
- time. On bare hardware MicroPython has no provision for DST. I do not plan to
206
- adapt ` cron.py ` to account for this: its design focus is small lightweight code
207
- to run on bare metal targets. I could adapt ` crontest.py ` but it would surely
208
- fail in other countries.
217
+ The ` crontest.py ` test program produces failures under Unix. These result from
218
+ the fact that the Unix ` localtime ` function handles daylight saving time. The
219
+ purpose of ` crontest.py ` is to check ` cron ` code. It should be run on bare
220
+ metal targets.
209
221
210
222
##### [ Top] ( ./SCHEDULE.md#0-contents )
211
223
@@ -267,11 +279,29 @@ finally:
267
279
268
280
##### [ Top] ( ./SCHEDULE.md#0-contents )
269
281
270
- # 6. Use in synchronous code
282
+ # 6. Hardware timing limitations
283
+
284
+ The code has been tested on Pyboard 1.x, Pyboard D, ESP32 and ESP8266. All
285
+ except ESP8266 have good timing performance. Pyboards can be calibrated to
286
+ timepiece precision using a cheap DS3231 and
287
+ [ this utility] ( https://github.com/peterhinch/micropython-samples/tree/master/DS3231 ) .
288
+
289
+ The ESP8266 has poor time stability so is not well suited to long term timing
290
+ applications. On my reference board timing drifted by 1.4mins/hr, an error of
291
+ 2.3%.
292
+
293
+ Boards with internet connectivity can periodically synchronise to an NTP server
294
+ but this carries a risk of sudden jumps in the system time which may disrupt
295
+ ` uasyncio ` and the scheduler.
296
+
297
+ ##### [ Top] ( ./SCHEDULE.md#0-contents )
298
+
299
+ # 7. Use in synchronous code
271
300
272
301
It is possible to use the ` cron ` closure in synchronous code. This involves
273
- writing an event loop, an example of which is illustrated below. In this
274
- example a task list entry is a tuple with the following contents.
302
+ the mildly masochistic task of writing an event loop, an example of which is
303
+ illustrated below. In this example a task list entry is a tuple with the
304
+ following contents.
275
305
1 . The ` cron ` instance.
276
306
2 . The callback to run.
277
307
3 . A tuple of arguments for the callback.
@@ -312,7 +342,7 @@ def main():
312
342
for task in (t for t in tasks if t[0 ](now) == deltat): # Tasks with same delta t
313
343
to_run.append(task)
314
344
task[4 ] = True # Has been scheduled
315
- # Remove on -shot tasks which have been scheduled
345
+ # Remove one -shot tasks which have been scheduled
316
346
tasks = [t for t in tasks if not (t[3 ] and t[4 ])]
317
347
sleep(deltat)
318
348
for tsk in to_run:
@@ -325,23 +355,6 @@ main()
325
355
In my opinion the asynchronous version is cleaner and easier to understand. It
326
356
is also more versatile because the advanced features of ` uasyncio ` are
327
357
available to the application. The above code is incompatible with ` uasyncio `
328
- because of the blocking calls to ` time.sleep ` .
329
-
330
- ##### [ Top] ( ./SCHEDULE.md#0-contents )
331
-
332
- # 7. Hardware timing limitations
333
-
334
- The code has been tested on Pyboard 1.x, Pyboard D, ESP32 and ESP8266. All
335
- except ESP8266 have good timing performance. Pyboards can be calibrated to
336
- timepiece precision using a cheap DS3231 and
337
- [ this utility] ( https://github.com/peterhinch/micropython-samples/tree/master/DS3231 ) .
338
-
339
- The ESP8266 has poor time stability so is not well suited to long term timing
340
- applications. On my reference board timing drifted by 1.4mins/hr, an error of
341
- 2.3%.
342
-
343
- Boards with internet connectivity can periodically synchronise to an NTP server
344
- but this carries a risk of sudden jumps in the system time which may disrupt
345
- ` uasyncio ` and the scheduler.
358
+ because of the blocking calls to ` time.sleep() ` .
346
359
347
360
##### [ Top] ( ./SCHEDULE.md#0-contents )
0 commit comments