Skip to content

Commit e9be6bf

Browse files
committed
Tutorial: Note about Stream.drain concurrency and Barrier.
1 parent 4d5c050 commit e9be6bf

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

v3/docs/TUTORIAL.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,9 @@ the [Message class](./TUTORIAL.md#39-message) uses this approach to provide an
987987

988988
## 3.7 Barrier
989989

990-
This is an unofficial primitive and has no counterpart in CPython asyncio. It
991-
is based on a Microsoft primitive. While similar in purpose to `gather` there
992-
are differences described below.
990+
This is an unofficial implementation of a primitive supported in
991+
[CPython 3.11](https://docs.python.org/3.11/library/asyncio-sync.html#asyncio.Barrier).
992+
While similar in purpose to `gather` there are differences described below.
993993

994994
Its principal purpose is to cause multiple coros to rendezvous at a particular
995995
point. For example producer and consumer coros can synchronise at a point where
@@ -2000,10 +2000,12 @@ asyncio.run(main())
20002000
Writing to a `StreamWriter` occurs in two stages. The synchronous `.write`
20012001
method concatenates data for later transmission. The asynchronous `.drain`
20022002
causes transmission. To avoid allocation call `.drain` after each call to
2003-
`.write`. Do not have multiple tasks calling `.drain` concurrently: this can
2003+
`.write`. If multiple tasks are to write to the same `StreamWriter`, the best
2004+
solution is to implement a shared `Queue`. Each task writes to the `Queue` and
2005+
a single task waits on it, issuing `.write` and `.drain` whenever data is
2006+
queued. Do not have multiple tasks calling `.drain` concurrently: this can
20042007
result in data corruption for reasons detailed
2005-
[here](https://github.com/micropython/micropython/issues/6621). The solution is
2006-
to use a `Queue` or a `Lock`.
2008+
[here](https://github.com/micropython/micropython/issues/6621).
20072009

20082010
The mechanism works because the device driver (written in C) implements the
20092011
following methods: `ioctl`, `read`, `readline` and `write`. See

0 commit comments

Comments
 (0)