Skip to main content

Networking made simply using nng

Project description

This is pynng.

Ergonomic bindings for nanomsg next generation (nng), in Python. pynng provides a nice interface on top of the full power of nng. nng, and therefore pynng, make it easy to communicate between processes on a single computer or computers across a network.

Goals

Provide a Pythonic, works-out-of-the box library on Windows and Unix-y platforms. Like nng itself, the license is MIT, so it can be used without restriction.

Installation

On Windows and 64-bit Linux, the usual

pip install pynng

should suffice. Building from source is a little convoluted, due to some issues with the way the setup.py script is written. Nevertheless, it can be done:

git clone https://github.com/codypiersall/pynng
cd pynng
pip install .
python setup.py build
python setup.py build_ext --inplace
pytest

Installing on Mac

This project does not yet know how to build for Mac, because I don't have a Mac to test on. The tricky bit is letting cffi know the correct object file to link to, and ensuring whatever the Mac equivalent of -fPIC is set when compiling.

Using pynng

Using pynng is easy peasy:

from pynng import Pair0

s1 = Pair0()
s1.listen('tcp://127.0.0.1:54321')
s2 = Pair0()
s2.dial('tcp://127.0.0.1:54321')
s1.send(b'Well hello there')
print(s2.recv())
s1.close()
s2.close()

Since pynng sockets support setting most parameters in the socket's __init__ method and is a context manager, the above code can be written much shorter:

from pynng import Pair0

with Pair0(listen='tcp://127.0.0.1:54321') as s1, \
        Pair0(dial='tcp://127.0.0.1:54321') as s2:
    s1.send(b'Well hello there')
    print(s2.recv())

Using pynng with an async framework

Asynchronous sending also works with

trio and asyncio. Here is an example using trio:

import pynng
import trio

async def send_and_recv(sender, receiver, message):
    await sender.asend(message)
    return await receiver.arecv()

with pynng.Pair0(listen='tcp://127.0.0.1:54321') as s1, \
        pynng.Pair0(dial='tcp://127.0.0.1:54321') as s2:
    received = trio.run(send_and_recv, s1, s2, b'hello there old pal!')
    assert received == b'hello there old pal!'

Many other protocols are available as well:

  • 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.

TODO

  • Support Mac
  • More docs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynng-0.2.0.tar.gz (578.0 kB view hashes)

Uploaded Source

Built Distributions

pynng-0.2.0-cp37-cp37m-win_amd64.whl (176.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pynng-0.2.0-cp37-cp37m-win32.whl (165.8 kB view hashes)

Uploaded CPython 3.7m Windows x86

pynng-0.2.0-cp37-cp37m-manylinux1_x86_64.whl (294.5 kB view hashes)

Uploaded CPython 3.7m

pynng-0.2.0-cp36-cp36m-win_amd64.whl (176.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pynng-0.2.0-cp36-cp36m-win32.whl (165.8 kB view hashes)

Uploaded CPython 3.6m Windows x86

pynng-0.2.0-cp36-cp36m-manylinux1_x86_64.whl (294.5 kB view hashes)

Uploaded CPython 3.6m

pynng-0.2.0-cp35-cp35m-win_amd64.whl (176.1 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

pynng-0.2.0-cp35-cp35m-win32.whl (165.8 kB view hashes)

Uploaded CPython 3.5m Windows x86

pynng-0.2.0-cp35-cp35m-manylinux1_x86_64.whl (294.5 kB view hashes)

Uploaded CPython 3.5m

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page