Skip to content

Commit 33dfe3e

Browse files
committed
Tutorial: Add note on StreamReader read methods.
1 parent 69f4d11 commit 33dfe3e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

v3/docs/TUTORIAL.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,12 @@ asyncio.run(run())
20142014

20152015
## 6.3 Using the stream mechanism
20162016

2017+
A stream is an abstraction of a device whose interface consists of a realtime
2018+
source of bytes. Examples include UARTs, I2S devices and sockets. Many streams
2019+
are continuous: an I2S microphone will source data until switched off and the
2020+
interface is closed. Streams are supported by `asyncio.StreamReader` and
2021+
`asyncio.StreamWriter` classes.
2022+
20172023
This section applies to platforms other than the Unix build. The latter handles
20182024
stream I/O in a different way described
20192025
[here](https://github.com/micropython/micropython/issues/7965#issuecomment-960259481).
@@ -2054,6 +2060,7 @@ async def main():
20542060

20552061
asyncio.run(main())
20562062
```
2063+
The `.readline` method will pause until `\n` is received. The `.read`
20572064
Writing to a `StreamWriter` occurs in two stages. The synchronous `.write`
20582065
method concatenates data for later transmission. The asynchronous `.drain`
20592066
causes transmission. To avoid allocation call `.drain` after each call to
@@ -2078,6 +2085,12 @@ avoid buffer overflows and data loss. This can be ameliorated by using a larger
20782085
UART read buffer or a lower baudrate. Alternatively hardware flow control will
20792086
provide a solution if the data source supports it.
20802087

2088+
The `StreamReader` read methods fall into two categories depending on whether
2089+
they wait for a specific end condition. Thus `.readline` pauses until a newline
2090+
byte has been received, `.read(-1)` waits for EOF, and `readexactly` waits for
2091+
a precise number of bytes. Other methods return the number of bytes available
2092+
at the time they are called (upto a maximum).
2093+
20812094
### 6.3.1 A UART driver example
20822095

20832096
The program [auart_hd.py](../as_demos/auart_hd.py) illustrates a method of

0 commit comments

Comments
 (0)