Skip to content

Commit 4001e28

Browse files
committed
broker.py: Provide instance. Pub message defaults to None.
1 parent 8e01d42 commit 4001e28

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

v3/docs/DRIVERS.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,14 +1139,15 @@ finally:
11391139
# 9. Message Broker
11401140

11411141
```python
1142-
from primitives import Broker # broker.py
1142+
from primitives import Broker, broker # broker.py
11431143
```
11441144
The `Broker` class provides a flexible means of messaging between running tasks.
11451145
It uses a publish-subscribe model (akin to MQTT) whereby the transmitting task
11461146
publishes to a topic. Objects subscribed to that topic will receive the message.
11471147
This enables one to one, one to many, many to one or many to many messaging.
11481148

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
11501151
the broker publishes a message, every `agent` subscribed to the message topic
11511152
will be triggered. In the simplest case the `agent` is a `Queue` instance: the
11521153
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
11591160
is not threadsafe: `Broker` methods should not be called from a hard ISR or from
11601161
another thread.
11611162

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+
11621169
#### Broker methods
11631170

11641171
All are synchronous.
@@ -1168,12 +1175,17 @@ with a matching `topic`. Any additional args will be passed to the `agent` when
11681175
it is triggered.
11691176
* `unsubscribe(topic, agent, *args)` The `agent` will stop being triggered. If
11701177
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`.
11741181

11751182
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.
11771189

11781190
#### Agent types
11791191

@@ -1198,16 +1210,11 @@ Note that synchronous `agent` instances must run to completion quickly otherwise
11981210
the `publish` method will be slowed. See [Notes](./DRIVERS.md#93-notes) for
11991211
further details on queue behaviour.
12001212

1201-
#### Broker class variable
1202-
1203-
* `Verbose=True` Enables printing of debug messages.
1204-
12051213
#### example
12061214
```py
12071215
import asyncio
1208-
from primitives import Broker, RingbufQueue
1216+
from primitives import broker, RingbufQueue
12091217

1210-
broker = Broker()
12111218
async def sender(t):
12121219
for x in range(t):
12131220
await asyncio.sleep(1)
@@ -1245,11 +1252,10 @@ The following illustrates a use case for passing args to an `agent` (pin nos.
12451252
are for Pyoard 1.1).
12461253
```py
12471254
import asyncio
1248-
from primitives import Broker
1255+
from primitives import broker
12491256
from machine import Pin
12501257
red = Pin("A13", Pin.OUT, value=0) # Pin nos. for Pyboard V1.1
12511258
green = Pin("A14", Pin.OUT, value=0)
1252-
broker = Broker()
12531259

12541260
async def flash():
12551261
broker.publish("led", 1)
@@ -1275,9 +1281,8 @@ asyncio.run(main())
12751281
A task can wait on multiple topics using a `RingbufQueue`:
12761282
```python
12771283
import asyncio
1278-
from primitives import Broker, RingbufQueue
1284+
from primitives import broker, RingbufQueue
12791285

1280-
broker = Broker()
12811286

12821287
async def receiver():
12831288
q = RingbufQueue(10)
@@ -1316,9 +1321,8 @@ should run to completion quickly.
13161321

13171322
```py
13181323
import asyncio
1319-
from primitives import Broker, Agent
1324+
from primitives import broker, Agent
13201325

1321-
broker = Broker()
13221326
class MyAgent(Agent):
13231327
def put(sef, topic, message, arg):
13241328
print(f"User agent. Topic: {topic} Message: {message} Arg: {arg}")

v3/primitives/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def _handle_exception(loop, context):
5454
"Keyboard": "sw_array",
5555
"SwArray": "sw_array",
5656
"Broker": "broker",
57+
"broker": "broker",
5758
"Agent": "broker",
5859
"RegExp": "broker",
5960
}

v3/primitives/broker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# broker.py A message broker for MicroPython
22

3-
# Copyright (c) 2024 Peter Hinch
3+
# Copyright (c) 2024-2025 Peter Hinch
44
# Released under the MIT License (MIT) - see LICENSE file
55

66
# Inspired by the following
@@ -11,7 +11,7 @@
1111
import re
1212

1313

14-
class Agent:
14+
class Agent: # ABC for user agent
1515
pass
1616

1717

@@ -58,7 +58,7 @@ def unsubscribe(self, topic, agent, *args):
5858
elif Broker.Verbose:
5959
print(f"Unsubscribe topic {topic} fail: topic not subscribed.")
6060

61-
def publish(self, topic, message):
61+
def publish(self, topic, message=None):
6262
agents = set() # Agents which are triggered by this topic
6363
if isinstance(topic, str): # Check regexps
6464
# Are any keys RegExp instances?
@@ -84,3 +84,6 @@ def publish(self, topic, message):
8484
res = agent(topic, message, *args)
8585
if isinstance(res, type_coro):
8686
asyncio.create_task(res)
87+
88+
89+
broker = Broker()

v3/primitives/tests/broker_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
# import primitives.tests.broker_test
44

55
import asyncio
6-
from primitives import Broker, Queue, RingbufQueue, RegExp
7-
8-
broker = Broker()
6+
from primitives import broker, Queue, RingbufQueue, RegExp
97

108
# Periodically publish messages to two topics
119
async def test(t):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy