Code Monkey home page Code Monkey logo

pynng's Introduction

This is pynng.

MIT License PyPI Version Build succeeding Wheels building docs

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. This library is compatible with Python โ‰ฅ 3.5. nng is the rewriting of Nanomsg, which is the spiritual successor to ZeroMQ.

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, MacOS, and Linux, the usual

pip3 install pynng

should suffice. Note that on 32-bit Linux and on macOS no binary distributions are available, so CMake is also required.

Building from the GitHub repo works as well, natch:

git clone https://github.com/codypiersall/pynng
cd pynng
pip3 install -e .

(If you want to run tests, you also need to pip3 install trio curio pytest pytest-asyncio pytest-trio pytest-curio, then just run pytest.)

pynng might work on the BSDs as well. Who knows!

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

curio, 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.

Examples

Some examples (okay, just two examples) are available in the examples directory.

Git Branch Policy

The only stable branch is master. There will never be a git push -f on master. On the other hand, all other branches are not considered stable; they may be deleted, rebased, force-pushed, and any other manner of funky business.

pynng's People

Contributors

codypiersall avatar xzz53 avatar geronimo-iia avatar matthiasharrer avatar muthukumarclay avatar andrei-pozolotin avatar dahlia avatar wtfuzz avatar mprat avatar neachdainn avatar raphaelahrens avatar yamrzou avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.