@@ -1139,14 +1139,15 @@ finally:
1139
1139
# 9. Message Broker
1140
1140
1141
1141
``` python
1142
- from primitives import Broker # broker.py
1142
+ from primitives import Broker, broker # broker.py
1143
1143
```
1144
1144
The ` Broker ` class provides a flexible means of messaging between running tasks.
1145
1145
It uses a publish-subscribe model (akin to MQTT) whereby the transmitting task
1146
1146
publishes to a topic. Objects subscribed to that topic will receive the message.
1147
1147
This enables one to one, one to many, many to one or many to many messaging.
1148
1148
1149
- A task subscribes to a topic via an ` agent ` . This is stored by the broker. When
1149
+ A task subscribes to a topic via an ` agent ` : this term describes a set of Python
1150
+ types which may be used in this role. An ` agent ` is stored by the broker. When
1150
1151
the broker publishes a message, every ` agent ` subscribed to the message topic
1151
1152
will be triggered. In the simplest case the ` agent ` is a ` Queue ` instance: the
1152
1153
broker puts the topic and message onto the subscriber's queue for retrieval.
@@ -1159,6 +1160,12 @@ no "knowledge" of the number or type of agents subscribed to a topic. The module
1159
1160
is not threadsafe: ` Broker ` methods should not be called from a hard ISR or from
1160
1161
another thread.
1161
1162
1163
+ A ` Broker ` instance ` broker ` is provided. Where multiple modules issue
1164
+ ``` python
1165
+ from primitives import broker
1166
+ ```
1167
+ all will see the same instance, facilitating message passing between modules.
1168
+
1162
1169
#### Broker methods
1163
1170
1164
1171
All are synchronous.
@@ -1168,12 +1175,17 @@ with a matching `topic`. Any additional args will be passed to the `agent` when
1168
1175
it is triggered.
1169
1176
* ` unsubscribe(topic, agent, *args) ` The ` agent ` will stop being triggered. If
1170
1177
args were passed on subscription, the same args must be passed.
1171
- * ` publish(topic, message) ` All ` agent ` instances subscribed to ` topic ` will be
1172
- triggered, receiving ` topic ` and ` message ` plus any further args that were
1173
- passed to ` subscribe ` .
1178
+ * ` publish(topic, message=None ) ` All ` agent ` instances subscribed to ` topic `
1179
+ will be triggered, receiving ` topic ` and ` message ` plus any further args that
1180
+ were passed to ` subscribe ` .
1174
1181
1175
1182
The ` topic ` arg is typically a string but may be any hashable object. A
1176
- ` message ` is an arbitrary Python object.
1183
+ ` message ` is an arbitrary Python object. Where string topics are used, wildcard
1184
+ subscriptions are possible.
1185
+
1186
+ #### Broker class variable
1187
+
1188
+ * ` Verbose=True ` Enables printing of debug messages.
1177
1189
1178
1190
#### Agent types
1179
1191
@@ -1198,16 +1210,11 @@ Note that synchronous `agent` instances must run to completion quickly otherwise
1198
1210
the ` publish ` method will be slowed. See [ Notes] ( ./DRIVERS.md#93-notes ) for
1199
1211
further details on queue behaviour.
1200
1212
1201
- #### Broker class variable
1202
-
1203
- * ` Verbose=True ` Enables printing of debug messages.
1204
-
1205
1213
#### example
1206
1214
``` py
1207
1215
import asyncio
1208
- from primitives import Broker , RingbufQueue
1216
+ from primitives import broker , RingbufQueue
1209
1217
1210
- broker = Broker()
1211
1218
async def sender (t ):
1212
1219
for x in range (t):
1213
1220
await asyncio.sleep(1 )
@@ -1245,11 +1252,10 @@ The following illustrates a use case for passing args to an `agent` (pin nos.
1245
1252
are for Pyoard 1.1).
1246
1253
``` py
1247
1254
import asyncio
1248
- from primitives import Broker
1255
+ from primitives import broker
1249
1256
from machine import Pin
1250
1257
red = Pin(" A13" , Pin.OUT , value = 0 ) # Pin nos. for Pyboard V1.1
1251
1258
green = Pin(" A14" , Pin.OUT , value = 0 )
1252
- broker = Broker()
1253
1259
1254
1260
async def flash ():
1255
1261
broker.publish(" led" , 1 )
@@ -1275,9 +1281,8 @@ asyncio.run(main())
1275
1281
A task can wait on multiple topics using a ` RingbufQueue ` :
1276
1282
``` python
1277
1283
import asyncio
1278
- from primitives import Broker , RingbufQueue
1284
+ from primitives import broker , RingbufQueue
1279
1285
1280
- broker = Broker()
1281
1286
1282
1287
async def receiver ():
1283
1288
q = RingbufQueue(10 )
@@ -1316,9 +1321,8 @@ should run to completion quickly.
1316
1321
1317
1322
``` py
1318
1323
import asyncio
1319
- from primitives import Broker , Agent
1324
+ from primitives import broker , Agent
1320
1325
1321
- broker = Broker()
1322
1326
class MyAgent (Agent ):
1323
1327
def put (sef , topic , message , arg ):
1324
1328
print (f " User agent. Topic: { topic} Message: { message} Arg: { arg} " )
0 commit comments