From 41e27750acc0817c81ea25921cb77a79935d5e1a Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 8 Dec 2024 07:25:37 +0000 Subject: [PATCH 1/3] use settings from trio for Selector waker socketpair --- src/anyio/_core/_asyncio_selector_thread.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/anyio/_core/_asyncio_selector_thread.py b/src/anyio/_core/_asyncio_selector_thread.py index d98c3040..f4d18cf0 100644 --- a/src/anyio/_core/_asyncio_selector_thread.py +++ b/src/anyio/_core/_asyncio_selector_thread.py @@ -21,6 +21,23 @@ def __init__(self) -> None: self._send, self._receive = socket.socketpair() self._send.setblocking(False) self._receive.setblocking(False) + # This somewhat reduces the amount of memory wasted queueing up data + # for wakeups. With these settings, maximum number of 1-byte sends + # before getting BlockingIOError: + # Linux 4.8: 6 + # macOS (darwin 15.5): 1 + # Windows 10: 525347 + # Windows you're weird. (And on Windows setting SNDBUF to 0 makes send + # blocking, even on non-blocking sockets, so don't do that.) + self._receive.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1) + self._send.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1) + # On Windows this is a TCP socket so this might matter. On other + # platforms this fails b/c AF_UNIX sockets aren't actually TCP. + try: + self._send.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + except OSError: + pass + self._selector.register(self._receive, EVENT_READ) self._closed = False From a61de1d785158f4f3f2c8d1b5e26b8c5819e6252 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Mon, 16 Dec 2024 07:18:57 +0000 Subject: [PATCH 2/3] add news --- docs/versionhistory.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 50745252..7f2af231 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -2,7 +2,11 @@ Version history =============== This library adheres to `Semantic Versioning 2.0 `_. - +**UNRELEASED** +- Configure ``SO_RCVBUF``, ``SO_SNDBUF`` and ``TCP_NODELAY`` on the selector + thread waker socket pair. This should improve the performance of ``wait_readable()`` + and ``wait_writable()`` when using the ``ProactorEventLoop`` + (`#836 `_) (PR by @graingert) **4.7.0** - Updated ``TaskGroup`` to work with asyncio's eager task factories From a9b6bb2274ed1103c8061eec75362b01523ace20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Mon, 16 Dec 2024 11:07:47 +0200 Subject: [PATCH 3/3] Update versionhistory.rst --- docs/versionhistory.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 7f2af231..518aef88 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -2,11 +2,14 @@ Version history =============== This library adheres to `Semantic Versioning 2.0 `_. + **UNRELEASED** + - Configure ``SO_RCVBUF``, ``SO_SNDBUF`` and ``TCP_NODELAY`` on the selector thread waker socket pair. This should improve the performance of ``wait_readable()`` and ``wait_writable()`` when using the ``ProactorEventLoop`` - (`#836 `_) (PR by @graingert) + (`#836 `_; PR by @graingert) + **4.7.0** - Updated ``TaskGroup`` to work with asyncio's eager task factories