From 0cc613390a10ef319d7ea10b5cbcd020b5d1a369 Mon Sep 17 00:00:00 2001 From: "Diez B. Roggisch" Date: Sat, 15 Jan 2022 14:47:39 +0100 Subject: [PATCH 1/2] Introduce block-argument to send By default, sending blocks. When block=False, raise pynng.TryAgain if no data could be delivered. --- pynng/nng.py | 19 ++++++++++++++++--- test/test_api.py | 6 ++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pynng/nng.py b/pynng/nng.py index 1b71a4e..426fd24 100644 --- a/pynng/nng.py +++ b/pynng/nng.py @@ -456,10 +456,23 @@ def recv(self, block=True): lib.nng_free(data[0], size_t[0]) return recvd - def send(self, data): - """Sends ``data`` (either ``bytes`` or ``bytearray``) on socket.""" + def send(self, data, block=True): + """Sends ``data`` on socket. + + Args: + + data: either ``bytes`` or ``bytearray`` + + block: If block is True (the default), the function will + not return until the operation is completed or times out. + If block is False, the function will raise ``pynng.TryAgain`` + immediately if no data was sent. + """ _ensure_can_send(data) - err = lib.nng_send(self.socket, data, len(data), 0) + flags = 0 + if not block: + flags |= lib.NNG_FLAG_NONBLOCK + err = lib.nng_send(self.socket, data, len(data), flags) check_err(err) async def arecv(self): diff --git a/test/test_api.py b/test/test_api.py index 13e7f94..d151637 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -57,6 +57,12 @@ def test_nonblocking_recv_works(): s.recv(block=False) +def test_nonblocking_send_works(): + with pynng.Pair0(listen=addr) as s: + with pytest.raises(pynng.TryAgain): + s.send(b'sad message, never will be seen', block=False) + + @pytest.mark.trio async def test_context(): with pynng.Req0(listen=addr, recv_timeout=1000) as req_sock, \ From dd4dbe14ef0d0c3d8c926c454d8c459980babeb2 Mon Sep 17 00:00:00 2001 From: "Diez B. Roggisch" Date: Sat, 16 Apr 2022 13:55:19 +0200 Subject: [PATCH 2/2] Update documentation on how to run tests properly --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bcbfa97..a0a60c7 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Building from the GitHub repo works as well, natch: 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`.) +then just run `pytest test`.) pynng might work on the BSDs as well. Who knows!