Example #1
0
def test_sub_sock_options():
    with pynng.Pub0(listen=addr) as pub:
        # test single option topic
        with pynng.Sub0(dial=addr, topics='beep', recv_timeout=500) as sub:
            wait_pipe_len(sub, 1)
            pub.send(b'beep hi')
            assert sub.recv() == b'beep hi'
        with pynng.Sub0(dial=addr, topics=['beep', 'hello'],
                        recv_timeout=500) as sub:
            wait_pipe_len(sub, 1)
            pub.send(b'beep hi')
            assert sub.recv() == b'beep hi'
            pub.send(b'hello there')
            assert sub.recv() == b'hello there'
Example #2
0
 def __init__(self, local_endpoint):
     self.local_endpoint = local_endpoint
     self.serializer = PickleSerializer()  # TODO set dynamically.
     self.sub = pynng.Sub0()
     self.sub.tcp_keepalive = True
     #self.sub.add_post_pipe_connect_cb(self.__on_connect)
     #self.sub.add_post_pipe_remove_cb(self.__on_disconnect)
     self.sub.dial('tcp://0.0.0.0:41001',
                   block=True)  # TODO - Set dynamically or via config
Example #3
0
async def client(addr, max_msg=10):
    with pynng.Sub0() as sock:
        sock.subscribe('')
        list(map(sock.dial, [f'{ADDRESS}/{_}' for _ in addr]))
        while max_msg:
            msg = await sock.arecv_msg()
            print(msg.bytes)
            # print(orjson.loads(msg.bytes.decode()))
            max_msg -= 1
Example #4
0
async def client(name, max_msg=2):
    with pynng.Sub0() as sock:
        sock.subscribe("")
        sock.dial(address)

        while max_msg:
            msg = await sock.arecv_msg()
            print(f"CLIENT ({name}): RECEIVED  {msg.bytes.decode()}")
            max_msg -= 1
Example #5
0
def test_pubsub0():
    with pynng.Sub0(listen=addr, recv_timeout=100) as sub, \
            pynng.Pub0(dial=addr, recv_timeout=100) as pub:

        sub.subscribe(b'')
        msg = b'i am requesting'
        time.sleep(0.04)
        pub.send(msg)
        assert sub.recv() == msg

        # TODO: when changing exceptions elsewhere, change here!
        # publishers can't recv
        with pytest.raises(pynng.NotSupported):
            pub.recv()

        # responders can't send before receiving
        with pytest.raises(pynng.NotSupported):
            sub.send(b"""I am a bold subscribing socket.  I believe I was truly
                         meant to be a publisher.  The world needs to hear what
                         I have to say!
                     """)
Example #6
0
    async def subs(which):
        if which == 'even':
            pred = is_even
        else:
            pred = lambda i: not is_even(i)

        with pynng.Sub0(dial=addr, recv_timeout=5000) as subber:
            subber.subscribe(which + ':')

            while True:
                val = await subber.arecv()
                print(val)

                lot, _, i = val.partition(b':')

                if i == b'None':
                    break

                assert pred(int(i))

            # mark subscriber as having received None sentinel
            sentinel_received[which] = True
Example #7
0
#!/user/local/bin/python3
import pynng
import time

addr = "tcp://127.0.0.1:2345"

with pynng.Sub0(listen=addr, recv_timeout=30000) as sub:
    #with pynng.Pub0(dial=addr, recv_timeout=100) as pub:

    sub.subscribe('')
    print('Receive messages from publisher')
    while True:
        line = str(sub.recv())
        print('recv> {}'.format(line))
        if line == 'quit':
            break
    sub.close()

if __name__ == '__main__':
    main()