Skip to content

Commit 66f03ed

Browse files
committed
v3/docs/SCHEDULE.md Various improvements.
1 parent 028525a commit 66f03ed

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

v3/docs/SCHEDULE.md

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
1. [Scheduling tasks](./SCHEDULE.md#1-scheduling-tasks)
44
2. [Overview](./SCHEDULE.md#2-overview)
55
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
77
4.1 [Time specifiers](./SCHEDULE.md#41-time-specifiers)
88
4.2 [The time to an event](./SCHEDULE.md#42-the-time-to-an-event)
99
4.3 [How it works](./SCHEDULE.md#43-how-it-works)
1010
4.4 [Calendar behaviour](./SCHEDULE.md#44-calendar-behaviour)
1111
4.5 [Limitations](./SCHEDULE.md#45-limitations)
1212
4.6 [The Unix build](./SCHEDULE.md#46-the-unix-build)
1313
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
1616

1717
##### [Tutorial](./TUTORIAL.md#contents)
1818
##### [Main V3 README](../README.md)
@@ -64,10 +64,24 @@ move to the parent directory of `sched` and issue:
6464
```
6565
Adapt the destination as appropriate for your hardware.
6666

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+
6780
# 4. The cron object
6881

6982
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.
7185

7286
It takes the following keyword-only args. A flexible set of data types are
7387
accepted. These are known as `Time specifiers` and described below. Valid
@@ -148,7 +162,7 @@ the month.
148162

149163
Specifying `wday=d` and `mday=n` where n > 22 could result in a day beyond the
150164
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.
152166

153167
### 4.4.2 Time causing month rollover
154168

@@ -165,7 +179,7 @@ will change to the next valid month. This code, run at 9am on 31st July, would
165179
aim to run the event at 1.59 on 1st October.
166180
```python
167181
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
169183
```
170184

171185
##### [Top](./SCHEDULE.md#0-contents)
@@ -184,14 +198,14 @@ depends on the complexity of the time specifiers.
184198

185199
On hardware platforms the MicroPython `time` module does not handle daylight
186200
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.
188202

189203
## 4.6 The Unix build
190204

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.
193207

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
195209
usable on Linux provided the daylight saving time (DST) constraints below are
196210
met.
197211

@@ -200,12 +214,10 @@ and duplicates when they go back. Scheduling those times will fail. A solution
200214
is to avoid scheduling the times in your region where this occurs (01.00.00 to
201215
02.00.00 in March and October here).
202216

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.
209221

210222
##### [Top](./SCHEDULE.md#0-contents)
211223

@@ -267,11 +279,29 @@ finally:
267279

268280
##### [Top](./SCHEDULE.md#0-contents)
269281

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
271300

272301
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.
275305
1. The `cron` instance.
276306
2. The callback to run.
277307
3. A tuple of arguments for the callback.
@@ -312,7 +342,7 @@ def main():
312342
for task in (t for t in tasks if t[0](now) == deltat): # Tasks with same delta t
313343
to_run.append(task)
314344
task[4] = True # Has been scheduled
315-
# Remove on-shot tasks which have been scheduled
345+
# Remove one-shot tasks which have been scheduled
316346
tasks = [t for t in tasks if not (t[3] and t[4])]
317347
sleep(deltat)
318348
for tsk in to_run:
@@ -325,23 +355,6 @@ main()
325355
In my opinion the asynchronous version is cleaner and easier to understand. It
326356
is also more versatile because the advanced features of `uasyncio` are
327357
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()`.
346359

347360
##### [Top](./SCHEDULE.md#0-contents)

0 commit comments

Comments
 (0)