8000 GH-91166: zero copy SelectorSocketTransport transport implementation by kumaraditya303 · Pull Request #31871 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-91166: zero copy SelectorSocketTransport transport implementation #31871

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

Merged
merged 23 commits into from
Dec 24, 2022
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Next Next commit
fix writelines and add comments
  • Loading branch information
kumaraditya303 committed Nov 28, 2022
commit d1fae6cdd3b3ba20dc5a2e2bae2f468844b8071d
8000
33 changes: 18 additions & 15 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,16 +1080,16 @@ def write(self, data):
self._buffer.append(data)
self._maybe_pause_protocol()

def _get_sendmsg_buffer(self):
return itertools.islice(self._buffer, SC_IOV_MAX)
def _get_sendmsg_buffer(self, buffer: collections.deque):
return itertools.islice(buffer, SC_IOV_MAX)

def _write_sendmsg(self):
assert self._buffer, 'Data should not be empty'
if self._conn_lost:
return
try:
nbytes = self._sock.sendmsg(self._get_sendmsg_buffer())
self._adjust_leftover_buffer(nbytes)
nbytes = self._sock.sendmsg(self._get_sendmsg_buffer(self._buffer))
self._adjust_leftover_buffer(self._buffer, nbytes)
except (BlockingIOError, InterruptedError):
pass
except (SystemExit, KeyboardInterrupt):
Expand All @@ -1111,8 +1111,7 @@ def _write_sendmsg(self):
elif self._eof:
self._sock.shutdown(socket.SHUT_WR)

def _adjust_leftover_buffer(self, nbytes: int) -> None:
buffer = self._buffer
def _adjust_leftover_buffer(self, buffer: collections.deque, nbytes: int) -> None:
while nbytes:
b = buffer.popleft()
b_len = len(b)
Expand Down Expand Up @@ -1160,15 +1159,19 @@ def write_eof(self):
if not self._buffer:
self._sock.shutdown(socket.SHUT_WR)

def writelines(self, list_of_data):
if self._eof:
raise RuntimeError('Cannot call writelines() after write_eof()')
if self._empty_waiter is not None:
raise RuntimeError('unable to writelines; sendfile is in progress')
if not list_of_data:
return
self._buffer.extend([memoryview(i) for i in list_of_data])
self._loop._add_writer(self._sock_fd, self._write_ready)
if _HAS_SENDMSG:
# Use faster implementation with sendmsg() if available otherwise fallback
# to the default implementation of writelines in WriteTransport
def writelines(self, list_of_data):
if self._eof:
raise RuntimeError('Cannot call writelines() after write_eof()')
if self._empty_waiter is not None:
raise RuntimeError('unable to writelines; sendfile is in progress')
if not list_of_data:
return
self._buffer.extend([memoryview(i) for i in list_of_data])
self._write_sendmsg()


def can_write_eof(self):
return True
Expand Down
0