Skip to content

Commit 706f928

Browse files
committed
v3 tutorial: add note about polling vs interrupts.
1 parent da63626 commit 706f928

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

v3/docs/TUTORIAL.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ REPL.
7777
8.4 [Scheduling in uasyncio](./TUTORIAL.md#84-scheduling-in-uasyncio)
7878
8.5 [Why cooperative rather than pre-emptive?](./TUTORIAL.md#85-why-cooperative-rather-than-pre-emptive)
7979
8.6 [Communication](./TUTORIAL.md#86-communication)
80-
8.7 [Polling](./TUTORIAL.md#87-polling)
80+
9. [Polling vs Interrupts](./TUTORIAL.md#9-polling-vs-interrupts) A common
81+
source of confusion.
8182

8283
###### [Main README](../README.md)
8384

@@ -2558,12 +2559,34 @@ communications; in a cooperative system these are seldom required.
25582559

25592560
###### [Contents](./TUTORIAL.md#contents)
25602561

2561-
## 8.7 Polling
2562-
2563-
Some hardware devices such as the Pyboard accelerometer don't support
2564-
interrupts, and therefore must be polled (i.e. checked periodically). Polling
2565-
can also be used in conjunction with interrupt handlers: the interrupt handler
2566-
services the hardware and sets a flag. A task polls the flag: if it's set it
2567-
handles the data and clears the flag. A better approach is to use an `Event`.
2562+
# 9. Polling vs Interrupts
2563+
2564+
The role of interrupts in cooperative systems has proved to be a source of
2565+
confusion in the forum. The merit of an interrupt service routine (ISR) is that
2566+
it runs very soon after the event causing it. On a Pyboard, Python code may be
2567+
running 15μs after a hardware change, enabling prompt servicing of hardware and
2568+
accurate timing of signals.
2569+
2570+
The question arises whether it is possible to use interrupts to cause a task to
2571+
be scheduled at reduced latency. It is easy to show that, in a cooperative
2572+
scheduler, interrupts offer no latency benefit compared to polling the hardware
2573+
directly.
2574+
2575+
The reason for this is that a cooperative scheduler only schedules tasks when
2576+
another task has yielded control. Consider a system with a number of concurrent
2577+
tasks, where the longest any task blocks before yielding to the scheduler is
2578+
`N`ms. In such a system, even with an ideal scheduler, the worst-case latency
2579+
between a hardware event occurring and its handling task beingnscheduled is
2580+
`N`ms, assuming that the mechanism for detecting the event adds no latency of
2581+
its own.
2582+
2583+
In practice `N` is likely to be on the order of many ms. On fast hardware there
2584+
will be a negligible performance difference between polling the hardware and
2585+
polling a flag set by an ISR. On hardware such as ESP8266 and ESP32 the ISR
2586+
approach will probably be slower owing to the long and variable interrupt
2587+
latency of these platforms.
2588+
2589+
Using an ISR to set a flag is probably best reserved for situations where an
2590+
ISR is already needed for other reasons.
25682591

25692592
###### [Contents](./TUTORIAL.md#contents)

0 commit comments

Comments
 (0)