(NOTE: writeup in process, but reached public okness )  I was building a new python/raspberryPi project that needed to send messages between a sensor/effector server and gui clients over an IP network.   The system needed to run on a private network with no cloud access, so all the fancy new stuff for IoT systems connected to elsehwhere were off the table.   I needed something that did peer-to-peer communications in a variety of styles.   I found a good solution in pynng- python bindings for the NNG- Garrett D’Amore's nanomsg nextgen.   See that last link for a dive into nanomsg-NG and how it evolved from work with ZeroMQ and some other networking methods. 

NNG provides a robust framework and a number of good networking patterns:

  • Pair0: one-to-one, bidirectional communication.
  • Pair1: one-to-one, bidirectional communication, but also supporting polyamorous sockets
  • Pub0, Sub0: publish/subscribe sockets.   
  •  Surveyor0, Respondent0: Broadcast a survey to respondents, e.g. to find out what services are available.    
  • Req0, Rep0: request/response pattern.    
  • Push0, Pull0: Aggregate messages from multiple sources and load balance among many destinations.

For my project,i needed a publish/subscribe (Pub/Sub) and 'polyamorous' bidirectional messages.  the polyamorous aspect makes it easier to create a server that can respond to multiple request clients.

I also needed a solid asynchronous technique - multithreading, etc - which can be tricky in python.  I was again pleased to find (from the pynng pages) the Trio project.   "The Trio project's goal is to produce a production-quality, permissively licensed, async/await-native I/O library for Python. Like all async libraries, its main purpose is to help you write programs that do multiple things at the same time with parallelized I/O."

Downside to these finds was the lack of good working examples that could quickly be tested and then used to build a more elaborate system.  Working examples make for simple test rigs and proof of concept.    The documentation for pynng gives basic examples but these are unfortunately not in the git source.  The GitHub repo only has examples pair0.py and pair1_async.py.  So I extracted them and expanded a bit.

Here's what I now have:

(in process.... refer back to project github repo.  code for each needs a bit more commenting and should reference back to either the original examples or the docs stuff.   Then get into the pub/sub plus polyamorous Pair1 messaging.)

  • pair0Async.py very simple pair of pair0 sockets doing send/receive in a trio.run
  • pair1_Async.py: a mod (?) of the one in Examples
  • pair1_PolyAsync.py  :  mod of async polyamorus example in ReadTheDocs
  • pubsub_1SingleApp.py : basically pub/sub example from ReadTheDocs
  • pynng separate Pub/Sub tools
    • pubsub_2publishAsync.py: publisher code w/both sync and trio async loops
    • pubsub_2subscribe.py: subscriber code from ReadTheDocs on its own
    • pubsub_2subscribeAsync.py: subscriber example using Trio async (also has sync loop available)