Closed ReinhardDaemon closed 5 years ago
On Wed., Nov. 21, 2018, 05:27 Reinhard Daemon, notifications@github.com wrote:
Hello,
I have this (not working) listener:
def uhu_listener(payload): print ("uhu_listener:") print ("\t topic: ", ??? GET TOPIC WHICH CALLED ME ???) print("\t` payload: ", payload)
and this main entry:
if name == "main": payload = 3 pub.subscribe(uhu_listener, topicName="uhu1") pub.subscribe(uhu_listener, topicName="uhu2") pub.subscribe(uhu_listener, topicName="uhu3") pub.sendMessage("uhu2", payload=payload)
So, uhu_listener listens to 3 Topics (uhu1, uhu2 and uhu3).
Is there a way to determine the topic, which "called" the uhu_listener method?
BR, Reinhard
Yes in the docs search for AUTO_TOPIC. It's discussed in a couple places, such as https://pypubsub.readthedocs.io/en/latest/usage/usage_basic_tasks.html?highlight=%22Topic%20as%20Message%20Data%22
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/schollii/pypubsub/issues/23, or mute the thread https://github.com/notifications/unsubscribe-auth/ACenmEF7QVEeS7lzJAW-OsrWKGTLZ9X7ks5uxSqfgaJpZM4Ys03f .
Dear Schollii,
uhu_listener is registered to topics "uhu1", "uhu2" and "uhu3".
Please give me a complete example, how I have to change the signature of uhu_lilstener signature with AUTO_TOPIC to fulfill my requirements.
Thank you.
Please post what you have tried then I can show you how to fix. Note that in addition to that link in my previous reply, there are several examples if you search with Google, such as:
Hello Schollii,
I played around and found this solution:
from pubsub import pub
from pubsub.utils.notification import useNotifyByWriteFile useNotifyByWriteFile(prefix="DEBUG - PUBSUB:\r\n\t")
def uhu_listener(topic=pub.AUTO_TOPIC,payload=None): print ("uhu_listener:") print ("\ttopic: ", topic.getName()) print ("\tpayload: ", payload) pass
if name == "main": pub.subscribe(uhu_listener, topicName="uhu1") pub.subscribe(uhu_listener, topicName="uhu2") pub.sendMessage("uhu1", payload=111) # --> uhu_listener pub.sendMessage("uhu2", payload=222) # --> uhu_listener pub.sendMessage("uhu3", payload=333) # --> to void
Am I doing it properly?
BR!
Looks good to me, does it print what you expect?
Dear Schollii,
yes, it works as expected.
I thought before that I have to use: def uhu_listener(topic=pub.AUTO_TOPIC... only in conjunction with: pub.subscribe(uhu_listener, pub.ALL_TOPICS)
Now I know better, thanks to your help.
I like the pub/sub concept a lot and already made one project that uses the cheap ESP8266 WiFi modules and a free of charger cloud broker (Flespi) to communicate over air, without IP addressing. Project is at: https://sourceforge.net/projects/esp8266-flespi-yum/
Now I see how easy and clear it is to use pub/sub within one python project in order to seperate the different tasks from one another and implementing a MVC pattern with it.
Thanks a lot for your great software lib! Reinhard
@ReinhardDaemon That's fabulous to hear, thanks for the feedback! I hope you are using v4.0?
Seems like I can close this issue, please re-open if I misunderstood.
Hello,
I have this (not working) listener:
and this main entry:
So, uhu_listener listens to 3 Topics (uhu1, uhu2 and uhu3).
Is there a way to determine the topic, which "called" the uhu_listener method?
BR, Reinhard