@@ -46,9 +46,8 @@ def __init__(self, timeout, callback, loop):
46
46
self ._handle = asyncio .run_coroutine_threadsafe (delayed , loop = loop )
47
47
48
48
@staticmethod
49
- @asyncio .coroutine
50
- def _call_delayed_coro (timeout , callback , loop ):
51
- yield from asyncio .sleep (timeout , loop = loop )
49
+ async def _call_delayed_coro (timeout , callback , loop ):
50
+ await asyncio .sleep (timeout , loop = loop )
52
51
return callback ()
53
52
54
53
def __lt__ (self , other ):
@@ -136,8 +135,7 @@ def close(self):
136
135
self ._close (), loop = self ._loop
137
136
)
138
137
139
- @asyncio .coroutine
140
- def _close (self ):
138
+ async def _close (self ):
141
139
log .debug ("Closing connection (%s) to %s" % (id (self ), self .endpoint ))
142
140
if self ._write_watcher :
143
141
self ._write_watcher .cancel ()
@@ -174,40 +172,39 @@ def push(self, data):
174
172
# avoid races/hangs by just scheduling this, not using threadsafe
175
173
self ._loop .create_task (self ._push_msg (chunks ))
176
174
177
- @asyncio .coroutine
178
- def _push_msg (self , chunks ):
175
+ async def _push_msg (self , chunks ):
179
176
# This lock ensures all chunks of a message are sequential in the Queue
180
- with ( yield from self ._write_queue_lock ) :
177
+ with await self ._write_queue_lock :
181
178
for chunk in chunks :
182
179
self ._write_queue .put_nowait (chunk )
183
180
184
181
185
- @asyncio .coroutine
186
- def handle_write (self ):
182
+ async def handle_write (self ):
187
183
while True :
188
184
try :
189
- next_msg = yield from self ._write_queue .get ()
185
+ next_msg = await self ._write_queue .get ()
190
186
if next_msg :
191
- yield from self ._loop .sock_sendall (self ._socket , next_msg )
187
+ await self ._loop .sock_sendall (self ._socket , next_msg )
192
188
except socket .error as err :
193
189
log .debug ("Exception in send for %s: %s" , self , err )
194
190
self .defunct (err )
195
191
return
196
192
except asyncio .CancelledError :
197
193
return
198
194
199
- @asyncio .coroutine
200
- def handle_read (self ):
195
+ async def handle_read (self ):
201
196
while True :
202
197
try :
203
- buf = yield from self ._loop .sock_recv (self ._socket , self .in_buffer_size )
198
+ buf = await self ._loop .sock_recv (self ._socket , self .in_buffer_size )
204
199
self ._iobuf .write (buf )
205
200
# sock_recv expects EWOULDBLOCK if socket provides no data, but
206
201
# nonblocking ssl sockets raise these instead, so we handle them
207
202
# ourselves by yielding to the event loop, where the socket will
208
203
# get the reading/writing it "wants" before retrying
209
204
except (ssl .SSLWantWriteError , ssl .SSLWantReadError ):
210
- yield
205
+ # Apparently the preferred way to yield to the event loop from within
206
+ # a native coroutine based on https://github.com/python/asyncio/issues/284
207
+ await asyncio .sleep (0 )
211
208
continue
212
209
except socket .error as err :
213
210
log .debug ("Exception during socket recv for %s: %s" ,
0 commit comments