When I use the following code to subscribe to a topic, I can not use the same code to subscribe to some different topic. How can I subscribe to different topics?
@PubSub.subscribe('pubsub', 'EnergyManagement/CurrentPrice')
def on_match(self, peer, sender, bus, topic, headers, message):
@PubSub.subscribe('pubsub', 'EnergyManagement/futurePrice')
def on_match(self, peer, sender, bus, topic, headers, message):
You can do this two different ways:
You can use multiple calls to self.vip.pubsub.subscribe.
These calls have to happen after an agent has finished starting. As Amin mentions in his answer you can do this in an "onstart" method. This method can be used to change subscriptions dynamically anytime after an agent has started.
@Core.receiver('onstart')
def my_onstart_method(self, sender, **kwargs):
self.vip.pubsub.subscribe(peer='pubsub', prefix="path/to/topic1", callback=self.on_match)
self.vip.pubsub.subscribe(peer='pubsub', prefix="path/to/topic2", callback=self.on_match)
Or you can use multiple decorators on the same class method:
@PubSub.subscribe('pubsub', 'EnergyManagement/CurrentPrice')
@PubSub.subscribe('pubsub', 'EnergyManagement/futurePrice')
def on_match(self, peer, sender, bus, topic, headers, message):
pass