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

replace gen.coroutine usage with async def #6262

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 8 additions & 9 deletions distributed/batched.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ def __repr__(self):

__str__ = __repr__

@gen.coroutine
def _background_send(self):
async def _background_send(self):
while not self.please_stop:
try:
yield self.waker.wait(self.next_deadline)
await self.waker.wait(self.next_deadline)
self.waker.clear()
except gen.TimeoutError:
pass
Expand All @@ -91,7 +90,7 @@ def _background_send(self):
self.batch_count += 1
self.next_deadline = time() + self.interval
try:
nbytes = yield self.comm.write(
nbytes = await self.comm.write(
payload, serializers=self.serializers, on_error="raise"
)
if nbytes < 1e6:
Expand Down Expand Up @@ -142,8 +141,8 @@ def send(self, *msgs: dict) -> None:
if self.next_deadline is None:
self.waker.set()

@gen.coroutine
def close(self, timeout=None):

async def close(self, timeout=None):
"""Flush existing messages and then close comm

If set, raises `tornado.util.TimeoutError` after a timeout.
Expand All @@ -152,17 +151,17 @@ def close(self, timeout=None):
return
self.please_stop = True
self.waker.set()
yield self.stopped.wait(timeout=timeout)
await self.stopped.wait(timeout=timeout)
if not self.comm.closed():
try:
if self.buffer:
self.buffer, payload = [], self.buffer
yield self.comm.write(
await self.comm.write(
payload, serializers=self.serializers, on_error="raise"
)
except CommClosedError:
pass
yield self.comm.close()
await self.comm.close()

def abort(self):
if self.comm is None:
Expand Down
7 changes: 2 additions & 5 deletions distributed/comm/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from ssl import SSLCertVerificationError, SSLError
from typing import Any, ClassVar

from tornado import gen

try:
import ssl
except ImportError:
Expand Down Expand Up @@ -327,8 +325,7 @@ async def write(self, msg, serializers=None, on_error="message"):

return frames_nbytes_total

@gen.coroutine
def close(self):
async def close(self):
# We use gen.coroutine here rather than async def to avoid errors like
# Task was destroyed but it is pending!
# Triggered by distributed.deploy.tests.test_local::test_silent_startup
Expand All @@ -338,7 +335,7 @@ def close(self):
try:
# Flush the stream's write buffer by waiting for a last write.
if stream.writing():
yield stream.write(b"")
await stream.write(b"")
stream.socket.shutdown(socket.SHUT_RDWR)
except OSError:
pass
Expand Down