Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Low-level API: transports' send_all_from_iterable() method are more efficient #315

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix type hint on Windows
  • Loading branch information
francis-clairicia committed Jun 30, 2024
commit 57e8fb0ef08da8f1e2696f0af1d65ce531363549
11 changes: 8 additions & 3 deletions src/easynetwork/lowlevel/api_sync/transports/socket.py
Original file line number Diff line number Diff line change
@@ -130,17 +130,22 @@ def send_noblock(self, data: bytes | bytearray | memoryview) -> int:

@_utils.inherit_doc(base_selector.SelectorStreamTransport)
def send_all_from_iterable(self, iterable_of_data: Iterable[bytes | bytearray | memoryview], timeout: float) -> None:
if constants.SC_IOV_MAX <= 0 or not _utils.supports_socket_sendmsg(self.__socket):
if constants.SC_IOV_MAX <= 0:
return super().send_all_from_iterable(iterable_of_data, timeout)

socket = self.__socket
socket_fileno = self.__socket.fileno
if not _utils.supports_socket_sendmsg(socket):
return super().send_all_from_iterable(iterable_of_data, timeout)

buffers: deque[memoryview] = deque(map(memoryview, iterable_of_data))
del iterable_of_data

def try_sendmsg() -> int:
try:
return self.__socket.sendmsg(itertools.islice(buffers, constants.SC_IOV_MAX))
return socket.sendmsg(itertools.islice(buffers, constants.SC_IOV_MAX))
except (BlockingIOError, InterruptedError):
raise base_selector.WouldBlockOnWrite(self.__socket.fileno()) from None
raise base_selector.WouldBlockOnWrite(socket_fileno()) from None

while buffers:
sent, timeout = self._retry(try_sendmsg, timeout)