@@ -1136,8 +1136,6 @@ finally:
1136
1136
1137
1137
# 9. Message Broker
1138
1138
1139
- This is under development: please check for updates.
1140
-
1141
1139
``` python
1142
1140
from primitives import Broker # broker.py
1143
1141
```
@@ -1148,7 +1146,7 @@ message. This enables one to one, one to many, many to one or many to many
1148
1146
messaging.
1149
1147
1150
1148
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
1152
1150
will be triggered. In the simplest case the ` agent ` is a ` Queue ` instance: the
1153
1151
broker puts the topic and message onto the subscriber's queue for retrieval.
1154
1152
@@ -1173,18 +1171,20 @@ The `topic` arg is typically a string but may be any hashable object. A
1173
1171
1174
1172
#### Agent types
1175
1173
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.
1177
1176
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.
1179
1179
* ` 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
1183
1181
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.
1188
1188
* ` user_agent ` Instance of a user class. See user agents below.
1189
1189
* ` Event ` Set when a message is received.
1190
1190
@@ -1232,7 +1232,8 @@ async def messages(client):
1232
1232
broker.publish(topic.decode(), msg.decode())
1233
1233
```
1234
1234
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.
1236
1237
1237
1238
The following illustrates a use case for passing args to an ` agent ` (pin nos.
1238
1239
are for Pyoard 1.1).
@@ -1322,14 +1323,15 @@ applications this behaviour is preferable. In general `RingbufQueue` is
1322
1323
preferred as it is optimised for microcontroller use and supports retrieval by
1323
1324
an asynchronous iterator.
1324
1325
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.
1327
1329
1328
1330
#### exceptions
1329
1331
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.
1333
1335
1334
1336
The ` Broker ` class throws a ` ValueError ` if ` .subscribe ` is called with an
1335
1337
invalid ` agent ` type. There are a number of non-fatal conditions which can occur
0 commit comments