Skip to content

PYTHON-4770 Improve CPU overhead of async locks and latency on Windows TLS sendall #1866

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions pymongo/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def a_acquire(self, blocking: bool = True, timeout: float = -1) -> bool:
return False
if not blocking:
return False
await asyncio.sleep(0)
await asyncio.sleep(0.001)

def release(self) -> None:
self._lock.release()
Expand Down Expand Up @@ -96,7 +96,7 @@ async def acquire(self, blocking: bool = True, timeout: float = -1) -> bool:
return False
if not blocking:
return False
await asyncio.sleep(0)
await asyncio.sleep(0.001)

async def wait(self, timeout: Optional[float] = None) -> bool:
if timeout is not None:
Expand Down
10 changes: 9 additions & 1 deletion pymongo/network_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,26 @@ def _is_ready(fut: Future) -> None:
else:
# The default Windows asyncio event loop does not support loop.add_reader/add_writer:
# https://docs.python.org/3/library/asyncio-platforms.html#asyncio-platform-support
# Note: In PYTHON-4493 we plan to replace this code with asyncio streams.
async def _async_sendall_ssl(
sock: Union[socket.socket, _sslConn], buf: bytes, dummy: AbstractEventLoop
) -> None:
view = memoryview(buf)
total_length = len(buf)
total_sent = 0
# Backoff starts at 1ms, doubles on timeout up to 512ms, and halves on success
# down to 1ms.
backoff = 0.001
while total_sent < total_length:
try:
sent = sock.send(view[total_sent:])
except BLOCKING_IO_ERRORS:
await asyncio.sleep(0.5)
await asyncio.sleep(backoff)
sent = 0
if sent > 0:
backoff = max(backoff / 2, 0.001)
else:
backoff = min(backoff * 2, 0.512)
total_sent += sent


Expand Down
Loading