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 socketsPub0
,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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pynng-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8c9c9254e2e5433f8f268ca146898b4a734f73ca656a2800a6e9d125c31b3e0 |
|
MD5 | 28813ad5f914f855a1ba8a9c4d8e7d9c |
|
BLAKE2b-256 | 03c68d82877bbd13f64c339e9d72426d2f21bfddb116d5be6f73d362ea4365da |
Hashes for pynng-0.2.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf3a25f4f8e1860ca52ce656cbf0f9a26a0164339b667dc75ad905bf435324b7 |
|
MD5 | b87701748a5bf2e9117a51e957e9287a |
|
BLAKE2b-256 | d83b6dde793125b465216696363111b56e9801e449a01b55bbff9528e61e14ef |
Hashes for pynng-0.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72078e41af11771d9c68a3940ff5b4db1c890d3f376f00526ae1ab3efbb02486 |
|
MD5 | a3802d21e032fa5307df92297560fccc |
|
BLAKE2b-256 | c4ae3ac26611dd8c60b04135338560a9b25454a8545e8ca0a9f4a97ca7adda41 |
Hashes for pynng-0.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2939c0e9498d58164bce8501be61725f0feb84f1bd701abcb733a626e1fae22 |
|
MD5 | d12b45d8af2dc88b69ef122ac7647798 |
|
BLAKE2b-256 | 93f5baeedaa18bfa9628abb2b1af5a26597ff2afc0ed9c6e1468a8f4ff0071ab |
Hashes for pynng-0.2.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1167aef24fdf41dc5837ecfe9fcdf95f322b42630caca6b04dcfc1df0395e1ef |
|
MD5 | b4d0f7dc8d7d7e6c10e90c0a61d1c98f |
|
BLAKE2b-256 | b96a42863e91144c4b1d4ce9f59c99a5af5be3870f35305689773aae04c83597 |
Hashes for pynng-0.2.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 273a5a47420c8acfbc980265d49d72e602155f8401bb82da6c7c409048a5688b |
|
MD5 | b6e329ca0e9c5b8e167c9b9d02f77066 |
|
BLAKE2b-256 | 6f19c686d3a43fe31c5e72915e4634bfb47ac11516205875c4e36a80f8f6160d |
Hashes for pynng-0.2.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81e5caf7e9d910493b7b885623cedd935a3e98134d810a59cbe5e13a37a97b21 |
|
MD5 | 20abb0668aaca1f6b558c0bc1a08a67f |
|
BLAKE2b-256 | 1905d7e283af890c8ea3d258bd2c906cd2711298c8a1724727ec0183f257ba3f |
Hashes for pynng-0.2.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7f6aa4b08596564aa776ae1ec56b7cb4537570e4f2f639958b5d0dcc7792b8e |
|
MD5 | 2d61289dc5b4d970a5fade540b1cee2f |
|
BLAKE2b-256 | 16cddc743b053b6dd061523a44fa7f2f8ed9e5a15cb38e5958e88f2030cbbf55 |
Hashes for pynng-0.2.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 634d7e7546669d43ee0b569239bbaaa18aee073b25da556345d6f2b52e92707d |
|
MD5 | 660fb1b36dda22c3f3e022f0bf2f097f |
|
BLAKE2b-256 | 7f5392e41e71495caad3da4e38740d40ef3261bff1fbcfa7b2318b230bf9a75a |