migrating all to async syntax

This commit is contained in:
Yun Xu 2018-03-01 22:35:58 -08:00
parent d1a8e8b042
commit a2fc37121b
2 changed files with 24 additions and 26 deletions

View File

@ -16,10 +16,10 @@ class ReuseableTCPConnector(TCPConnector):
self.old_proto = None self.old_proto = None
if aiohttp.__version__ >= '3.0': if aiohttp.__version__ >= '3.0':
@asyncio.coroutine
def connect(self, req, traces=None): async def connect(self, req, traces=None):
new_conn = yield from super(ReuseableTCPConnector, self)\ new_conn = await super(ReuseableTCPConnector, self)\
.connect(req, traces=traces) .connect(req, traces=traces)
if self.old_proto is not None: if self.old_proto is not None:
if self.old_proto != new_conn._protocol: if self.old_proto != new_conn._protocol:
raise RuntimeError( raise RuntimeError(
@ -28,10 +28,10 @@ class ReuseableTCPConnector(TCPConnector):
self.old_proto = new_conn._protocol self.old_proto = new_conn._protocol
return new_conn return new_conn
else: else:
@asyncio.coroutine
def connect(self, req): async def connect(self, req):
new_conn = yield from super(ReuseableTCPConnector, self)\ new_conn = await super(ReuseableTCPConnector, self)\
.connect(req) .connect(req)
if self.old_proto is not None: if self.old_proto is not None:
if self.old_proto != new_conn._protocol: if self.old_proto != new_conn._protocol:
raise RuntimeError( raise RuntimeError(

View File

@ -28,19 +28,18 @@ class DelayableTCPConnector(TCPConnector):
acting_as = self._acting_as acting_as = self._acting_as
return getattr(acting_as, item) return getattr(acting_as, item)
@asyncio.coroutine async def start(self, connection, read_until_eof=False):
def start(self, connection, read_until_eof=False):
if self.send_task is None: if self.send_task is None:
raise RuntimeError("do a send() before you do a start()") raise RuntimeError("do a send() before you do a start()")
resp = yield from self.send_task resp = await self.send_task
self.send_task = None self.send_task = None
self.resp = resp self.resp = resp
self._acting_as = self.resp self._acting_as = self.resp
self.orig_start = getattr(resp, 'start') self.orig_start = getattr(resp, 'start')
try: try:
ret = yield from self.orig_start(connection, ret = await self.orig_start(connection,
read_until_eof) read_until_eof)
except Exception as e: except Exception as e:
raise e raise e
return ret return ret
@ -51,12 +50,11 @@ class DelayableTCPConnector(TCPConnector):
if self.send_task is not None: if self.send_task is not None:
self.send_task.cancel() self.send_task.cancel()
@asyncio.coroutine async def delayed_send(self, *args, **kwargs):
def delayed_send(self, *args, **kwargs):
req = self.req req = self.req
if self.delay and self.delay > 0: if self.delay and self.delay > 0:
#sync_sleep(self.delay) #sync_sleep(self.delay)
_ = yield from asyncio.sleep(self.delay) await asyncio.sleep(self.delay)
t = req.loop.time() t = req.loop.time()
print("sending at {}".format(t), flush=True) print("sending at {}".format(t), flush=True)
conn = next(iter(args)) # first arg is connection conn = next(iter(args)) # first arg is connection
@ -81,27 +79,27 @@ class DelayableTCPConnector(TCPConnector):
self._pre_request_delay = _pre_request_delay self._pre_request_delay = _pre_request_delay
if aiohttp.__version__ >= '3.0': if aiohttp.__version__ >= '3.0':
@asyncio.coroutine
def connect(self, req, traces=None): async def connect(self, req, traces=None):
d_req = DelayableTCPConnector.\ d_req = DelayableTCPConnector.\
RequestContextManager(req, self._pre_request_delay) RequestContextManager(req, self._pre_request_delay)
conn = yield from super(DelayableTCPConnector, self).connect(req, traces=traces) conn = await super(DelayableTCPConnector, self).connect(req, traces=traces)
if self._post_connect_delay and self._post_connect_delay > 0: if self._post_connect_delay and self._post_connect_delay > 0:
_ = yield from asyncio.sleep(self._post_connect_delay, await asyncio.sleep(self._post_connect_delay,
loop=self._loop) loop=self._loop)
req.send = d_req.send req.send = d_req.send
t = req.loop.time() t = req.loop.time()
print("Connected at {}".format(t), flush=True) print("Connected at {}".format(t), flush=True)
return conn return conn
else: else:
@asyncio.coroutine
def connect(self, req): async def connect(self, req):
d_req = DelayableTCPConnector.\ d_req = DelayableTCPConnector.\
RequestContextManager(req, self._pre_request_delay) RequestContextManager(req, self._pre_request_delay)
conn = yield from super(DelayableTCPConnector, self).connect(req) conn = await super(DelayableTCPConnector, self).connect(req)
if self._post_connect_delay and self._post_connect_delay > 0: if self._post_connect_delay and self._post_connect_delay > 0:
_ = yield from asyncio.sleep(self._post_connect_delay, await asyncio.sleep(self._post_connect_delay,
loop=self._loop) loop=self._loop)
req.send = d_req.send req.send = d_req.send
t = req.loop.time() t = req.loop.time()
print("Connected at {}".format(t), flush=True) print("Connected at {}".format(t), flush=True)