@@ -1173,14 +1173,10 @@ The `topic` arg is typically a string but may be any hashable object. A
1173
1173
1174
1174
#### Agent types
1175
1175
1176
- An ` agent ` may be any of the following:
1177
-
1178
- * ` Queue ` When a message is received it receives 2-tuple ` (topic, message) ` . If
1179
- extra args were passed on subscription the queue receives a 3-tuple.
1180
- ` (topic, message, (args...)) ` .
1181
- * ` RingbufQueue ` When a message is received it receives 2-tuple ` (topic, message) ` .
1182
- If extra args were passed on subscription it receives a 3-tuple,
1183
- ` (topic, message, (args...)) ` .
1176
+ An ` agent ` may be an instance of any of the following:
1177
+
1178
+ * ` RingbufQueue ` Received messages are queued as a 2-tuple ` (topic, message) ` .
1179
+ * ` Queue ` Received messages are queued as a 2-tuple ` (topic, message) ` .
1184
1180
* ` function ` Called when a message is received. Args: topic, message plus any
1185
1181
further args.
1186
1182
* ` bound method ` Called when a message is received. Args: topic, message plus any
@@ -1193,7 +1189,8 @@ message plus any further args.
1193
1189
* ` Event ` Set when a message is received.
1194
1190
1195
1191
Note that synchronous ` agent ` instances must run to completion quickly otherwise
1196
- the ` publish ` method will be slowed.
1192
+ the ` publish ` method will be slowed. See [ Notes] ( ./DRIVERS.md#93-notes ) for
1193
+ further details on queue behaviour.
1197
1194
1198
1195
#### Broker class variable
1199
1196
@@ -1202,24 +1199,25 @@ the `publish` method will be slowed.
1202
1199
#### example
1203
1200
``` py
1204
1201
import asyncio
1205
- from primitives import Broker, Queue
1202
+ from primitives import Broker, RingbufQueue
1206
1203
1207
1204
broker = Broker()
1208
- queue = Queue() # Or (e.g. RingbufQueue(20) )
1205
+ queue = RingbufQueue(20 )
1209
1206
async def sender (t ):
1210
1207
for x in range (t):
1211
1208
await asyncio.sleep(1 )
1212
1209
broker.publish(" foo_topic" , f " test { x} " )
1213
1210
1211
+ async def receiver ():
1212
+ async for topic, message in queue:
1213
+ print (topic, message)
1214
+
1214
1215
async def main ():
1215
1216
broker.subscribe(" foo_topic" , queue)
1216
- n = 10
1217
- asyncio.create_task(sender(n))
1218
- print (" Letting queue part-fill" )
1219
- await asyncio.sleep(5 )
1220
- for _ in range (n):
1221
- topic, message = await queue.get()
1222
- print (topic, message)
1217
+ rx = asyncio.create_task(receiver())
1218
+ await sender(10 )
1219
+ await asyncio.sleep(2 )
1220
+ rx.cancel()
1223
1221
1224
1222
asyncio.run(main())
1225
1223
```
@@ -1236,7 +1234,8 @@ async def messages(client):
1236
1234
Assuming the MQTT client is subscribed to multiple topics, message strings are
1237
1235
directed to individual tasks each supporting one topic.
1238
1236
1239
- The following illustrates a use case for ` agent ` args.
1237
+ The following illustrates a use case for passing args to an ` agent ` (pin nos.
1238
+ are for Pyoard 1.1).
1240
1239
``` py
1241
1240
import asyncio
1242
1241
from primitives import Broker
@@ -1319,17 +1318,23 @@ If a message causes a queue to fill, a message will silently be lost. It is the
1319
1318
responsibility of the subscriber to avoid this. In the case of a ` Queue `
1320
1319
instance the lost message is the one causing the overflow. In the case of
1321
1320
` RingbufQueue ` the oldest message in the queue is discarded. In some
1322
- applications this behaviour is preferable.
1321
+ applications this behaviour is preferable. In general ` RingbufQueue ` is
1322
+ preferred as it is optimised for microcontroller use and supports retrieval by
1323
+ an asynchronous iterator.
1324
+
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.
1323
1327
1324
1328
#### exceptions
1325
1329
1326
1330
An instance of an ` agent ` objects is owned by a subscribing tasks but is
1327
1331
executed by a publishing task. If a function used as an ` agent ` throws an
1328
1332
exception, the traceback will point to a ` Broker.publish ` call.
1329
1333
1330
- The ` Broker ` class does not throw exceptions. There are a number of non-fatal
1331
- conditions which can occur such as a queue overflow or an attempt to unsubscribe
1332
- an ` agent ` twice. The ` Broker ` will report these if ` Broker.Verboase=True ` .
1334
+ The ` Broker ` class throws a ` ValueError ` if ` .subscribe ` is called with an
1335
+ invalid ` agent ` type. There are a number of non-fatal conditions which can occur
1336
+ such as a queue overflow or an attempt to unsubscribe an ` agent ` twice. The
1337
+ ` Broker ` will report these if ` Broker.Verbose=True ` .
1333
1338
1334
1339
###### [ Contents] ( ./DRIVERS.md#0-contents )
1335
1340
0 commit comments