Publisher
Publisher
A publisher is a component that sends messages to one or more subscribers. Unlike a producer It's the publisher's responsibility to route the messages to the corresponding subscribers.
Methods:
Name | Description |
---|---|
register |
Registers one or more subscribers to the publisher. |
publish |
Publishes a message to one or more subscribers based on the topic. |
Example
from pymsgbus import Depends
from pymsgbus import Subscriber
from pymsgbus import Publisher
subscriber = Subscriber()
metricsdb = []
def metrics():
return metricsdb
@subscriber.subscribe('loss', 'accuracy')
def store_metric(metric, metrics: list = Depends(metrics)):
metrics.append(metric)
@subscriber.subscribe('accuracy')
def on_accuracy_to_high(metric):
if metric > 0.99:
raise StopIteration
publisher = Publisher()
publisher.register(subscriber)
publisher.publish(0.1, 'loss')
publisher.publish(0.9, 'accuracy')
assert metricsdb == [0.1, 0.9]
try:
publisher.publish(1.0, 'accuracy')
except StopIteration:
print("Early stopping")
publish(message, topic)
Publishes a message to one or more subscribers based on the topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Any
|
The message to publish. |
required |
topic
|
str
|
The topic to publish the message to. |
required |
register(*subscribers)
Registers one or more subscribers to the publisher.