schollii / pypubsub

A Python publish-subcribe library (moved here from SourceForge.net where I had it for many years)
189 stars 29 forks source link

Get the topic name, which "called" a listener #23

Closed ReinhardDaemon closed 5 years ago

ReinhardDaemon commented 5 years ago

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

schollii commented 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 .

ReinhardDaemon commented 5 years ago

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.

schollii commented 5 years ago

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:

ReinhardDaemon commented 5 years ago

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!

schollii commented 5 years ago

Looks good to me, does it print what you expect?

ReinhardDaemon commented 5 years ago

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

schollii commented 5 years ago

@ReinhardDaemon That's fabulous to hear, thanks for the feedback! I hope you are using v4.0?

schollii commented 5 years ago

Seems like I can close this issue, please re-open if I misunderstood.