pynng icon indicating copy to clipboard operation
pynng copied to clipboard

PUB/SUB example extended

Open douglasmccormickjr opened this issue 6 years ago • 6 comments

It seems like the PUB/SUB example could be improved (just a little bit)-- I'm getting confused that current example of pynng SUB + PUB code is all running in the same program (wouldn't this usually be running on 2 different machines, or multiple really, in practice?) I'd like to have 2 snippets of code that should be executed in separate threads/shells. The order of operation would be something like this....

(1) Run pynng_sub.py (2) Run pynng_pub.py (3) Results/msg would flow into sub based on the topics

I tried coming up with something but I'm getting some "pynng.exceptions.Timeout: Timed out" errors on the sub side...that leads me to another issue/topic related to this pattern...

Is there an elegant way to coordinate the PUB/SUB timing so the SUB is ready and will receive all the msg from PUB-- this is akin to the "Hello" msg sent to get acknowledgement that PUB and SUB are talking to each other (zmq docs mentioned this but didn't exactly show it completely, I can pull the link if it helps explain what I'm looking for better)...I'd love to know the right way to go about this so I don't hack something that's not optimal or resilient in the future.

douglasmccormickjr avatar May 21 '19 13:05 douglasmccormickjr

Thanks for creating this issue, @douglasmccormickjr.

It seems like the PUB/SUB example could be improved (just a little bit)

You're talking about the example in the docs, yes? It's helpful to have feedback about the usefulness/uselessness of examples, so thanks for providing that.

My goal for the examples in the docs was to show the simplest way to use the sockets; I am putting more advanced examples in the examples directory. I should probably figure out how to integrate those with the docs as well.

Is there an elegant way to coordinate the PUB/SUB timing so the SUB is ready and will receive all the msg from PUB

Are you talking about how all the pub/sub examples always have a sleep() call to give time for the connection to establish? If so, that's something that has bothered me too. In my tests I have a function that waits for connections to be established called wait_pipe_len. Clearly this is not part of the public API—it's not even part of installable package—but it's one way to skin this cat currently. I'm sure there are more...

codypiersall avatar May 22 '19 00:05 codypiersall

Aloha Cody - i started using pynng a week or two ago for a distributed data acquisition and control system based on RPi with clients elsewhere (windoze, etc). I like the pubSub setup but like douglas, would like some more examples. In particular, I'd like more info on Topics... how do we find out what the topic is for a received msg? how do we find what topics a subscriber is subscribed to? If we use sub.recv_msg(), what can we do with the returned "Message"?

I've been spelunking a bit into the nng c code but its been a couple decades since i worked at that python/c boundary, and there is a LOT to https://github.com/nanomsg/nng

I'd like to write a dispatch(msg) that would send the message off to the proper topic handler. So do I need to do my own parsing of msg=sub.recv() ? put a divider character at end of Topic when publishing, then index on that in received msg, splitting to {topic:data} perhaps?

MauiJerry avatar Jul 23 '19 05:07 MauiJerry

I circled back to this. My app was running ok with async on the server side but needed client so I had to get the example working. The attached zip file should have the two files needed. Note, you can swap commented sections to get it running sync or async

asyncPubSub.zip `

MauiJerry avatar Jul 30 '20 06:07 MauiJerry

I should probably figure out how to integrate those with the docs as well.

@codypiersall use sphinx's .. literalinclude::

This is what we've done with tractor's examples and as a bonus we actually run them as tests too :smile_cat:

goodboy avatar Jul 31 '20 22:07 goodboy

The attached zip file should have the two files needed.

@MauiJerry why don't you just make a pull request here with the suggested changes :wink:

goodboy avatar Jul 31 '20 22:07 goodboy

I built it against older version and have more advanced demos to share soon. Im work on a TK GUI that uses pynng, trio, TK. I'll get the real app built then circle back to documentation and simple demos

  • jerry isdale

sent from my phone, which may have added its own interpretation of what i typed or dictated. I apologize for any auto correct errors introduced by our robotic under lord.

On Fri, Jul 31, 2020, 12:04 PM goodboy [email protected] wrote:

The attached zip file should have the two files needed.

@MauiJerry https://github.com/MauiJerry why don't you just make a pull request here with the suggested changes 😉

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/codypiersall/pynng/issues/39#issuecomment-667405832, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALMU2K5S2GMMSQCNONXFUTR6M5W3ANCNFSM4HOK6WEA .

MauiJerry avatar Jul 31 '20 22:07 MauiJerry