8000 Docs: improve Broker coverage. · Angrite/micropython-async@e6f4a33 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6f4a33

Browse files
committed
Docs: improve Broker coverage.
1 parent dac5b88 commit e6f4a33

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

v3/docs/DRIVERS.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,6 @@ finally:
11361136

11371137
# 9. Message Broker
11381138

1139-
This is under development: please check for updates.
1140-
11411139
```python
11421140
from primitives import Broker # broker.py
11431141
```
@@ -1148,7 +1146,7 @@ message. This enables one to one, one to many, many to one or many to many
11481146
messaging.
11491147

11501148
A task subscribes to a topic with an `agent`. This is stored by the broker. When
1151-
the broker publishes a message, the `agent` of each task subscribed to its topic
1149+
the broker publishes a message, every `agent` subscribed to the message topic
11521150
will be triggered. In the simplest case the `agent` is a `Queue` instance: the
11531151
broker puts the topic and message onto the subscriber's queue for retrieval.
11541152

@@ -1173,18 +1171,20 @@ The `topic` arg is typically a string but may be any hashable object. A
11731171

11741172
#### Agent types
11751173

1176-
An `agent` may be an instance of any of the following:
1174+
An `agent` may be an instance of any of the following types. Args refers to any
1175+
arguments passed to the `agent`'s' subscription.
11771176

1178-
* `RingbufQueue` Received messages are queued as a 2-tuple `(topic, message)`.
1177+
* `RingbufQueue` Received messages are queued as a 2-tuple `(topic, message)`
1178+
assuming no args.
11791179
* `Queue` Received messages are queued as a 2-tuple `(topic, message)`.
1180-
* `function` Called when a message is received. Args: topic, message plus any
1181-
further args.
1182-
* `bound method` Called when a message is received. Args: topic, message plus any
1180+
* `function` Called when a message is received. Args: `topic`, `message` plus any
11831181
further args.
1184-
* `coroutine` Converted to a `task` when a message is received. Args: topic,
1185-
message plus any further args.
1186-
* `bound coroutine` Converted to a `task` when a message is received. Args: topic,
1187-
message plus any further args.
1182+
* `bound method` Called when a message is received. Args: `topic`, `message`
1183+
plus any further args.
1184+
* `coroutine` Converted to a `task` when a message is received. Args: `topic`,
1185+
`message` plus any further args.
1186+
* `bound coroutine` Converted to a `task` when a message is received. Args: `topic`,
1187+
`message` plus any further args.
11881188
* `user_agent` Instance of a user class. See user agents below.
11891189
* `Event` Set when a message is received.
11901190

@@ -1232,7 +1232,8 @@ async def messages(client):
12321232
broker.publish(topic.decode(), msg.decode())
12331233
```
12341234
Assuming the MQTT client is subscribed to multiple topics, message strings are
1235-
directed to individual tasks each supporting one topic.
1235+
directed to agents, each dedicated to handling a topic. An `agent` might operate
1236+
an interface or queue the message for a running task.
12361237

12371238
The following illustrates a use case for passing args to an `agent` (pin nos.
12381239
are for Pyoard 1.1).
@@ -1322,14 +1323,15 @@ applications this behaviour is preferable. In general `RingbufQueue` is
13221323
preferred as it is optimised for microcontroller use and supports retrieval by
13231324
an asynchronous iterator.
13241325

1325-
If either queue type is subscribed with args, publications will queued as a
1326-
3-tuple `(topic, message, (args...))`. There is no obvious use case for this.
1326+
If either queue type is subscribed with args, a publication will create a queue
1327+
entry that is a 3-tuple `(topic, message, (args...))`. There is no obvious use
1328+
case for this.
13271329

13281330
#### exceptions
13291331

1330-
An instance of an `agent` objects is owned by a subscribing tasks but is
1331-
executed by a publishing task. If a function used as an `agent` throws an
1332-
exception, the traceback will point to a `Broker.publish` call.
1332+
An `agent` instance is owned by a subscribing tasks but is executed by a
1333+
publishing task. If a function used as an `agent` throws an exception, the
1334+
traceback will point to a `Broker.publish` call.
13331335

13341336
The `Broker` class throws a `ValueError` if `.subscribe` is called with an
13351337
invalid `agent` type. There are a number of non-fatal conditions which can occur

v3/docs/TUTORIAL.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import uasyncio as asyncio
4545
3.7 [Barrier](./TUTORIAL.md#37-barrier)
4646
3.8 [Delay_ms](./TUTORIAL.md#38-delay_ms-class) Software retriggerable delay.
4747
3.9 [Message](./TUTORIAL.md#39-message)
48-
3.10 [Synchronising to hardware](./TUTORIAL.md#310-synchronising-to-hardware)
48+
3.10 [Message broker](./TUTORIAL.md#310-message-broker) A publish-subscribe model of messaging and control.
49+
3.11 [Synchronising to hardware](./TUTORIAL.md#311-synchronising-to-hardware)
4950
Debouncing switches, pushbuttons, ESP32 touchpads and encoder knobs. Taming ADC's.
5051
4. [Designing classes for asyncio](./TUTORIAL.md#4-designing-classes-for-asyncio)
5152
4.1 [Awaitable classes](./TUTORIAL.md#41-awaitable-classes)
@@ -589,6 +590,8 @@ following classes which are non-standard, are also in that directory:
589590
in a similar (but not identical) way to `gather`.
590591
* `Delay_ms` A useful software-retriggerable monostable, akin to a watchdog.
591592
Calls a user callback if not cancelled or regularly retriggered.
593+
* `RingbufQueue` a MicroPython-optimised queue.
594+
* `Broker` a means of messaging and control based on a publish/subscribe model.
592595

593596
A further set of primitives for synchronising hardware are detailed in
594597
[section 3.9](./TUTORIAL.md#39-synchronising-to-hardware).
@@ -1280,7 +1283,26 @@ provide an object similar to `Event` with the following differences:
12801283
It may be found in the `threadsafe` directory and is documented
12811284
[here](./THREADING.md#32-message).
12821285

1283-
## 3.10 Synchronising to hardware
1286+
## 3.10 Message broker
1287+
1288+
A `Broker` is a means of communicating data and/or control within or between
1289+
modules. It is typically a single global object, and uses a publish-subscribe
1290+
model. A publication comprises a `topic` and a `message`; the latter may be any
1291+
Python object. Tasks subscribe to a `topic` via an `agent` object. Whenever a
1292+
publication, occurs all `agent` instances currently subscribed to that topic are
1293+
triggered.
1294+
1295+
An `agent` may be an instance of various types including a function, a coroutine
1296+
or a queue.
1297+
1298+
A benefit of this approach is that the design of publishing tasks can proceed
1299+
independently from that of the subscribers; `agent` instances can be subscribed
1300+
and unsubscribed at run time with no effect on the publisher. The publisher
1301+
neither knows or cares about the type or number of subscribing `agent`s.
1302+
1303+
This is [documented here](https://github.com/peterhinch/micropython-async/blob/master/v3/docs/DRIVERS.md#9-message-broker).
1304+
1305+
## 3.11 Synchronising to hardware
12841306

12851307
The following hardware-related classes are documented [here](./DRIVERS.md):
12861308
* `ESwitch` A debounced switch with an `Event` interface.

0 commit comments

Comments
 (0)
0