39
39
pQuery = ql2_pb2 .Query .QueryType
40
40
41
41
42
- @asyncio .coroutine
43
- def _read_until (streamreader , delimiter ):
42
+ async def _read_until (streamreader , delimiter ):
44
43
"""Naive implementation of reading until a delimiter"""
45
44
buffer = bytearray ()
46
45
47
46
while True :
48
- c = yield from streamreader .read (1 )
47
+ c = yield streamreader .read (1 )
49
48
if c == b"" :
50
49
break # EOF
51
50
buffer .append (c [0 ])
52
51
if c == delimiter :
53
52
break
54
53
55
- return bytes (buffer )
54
+ yield bytes (buffer )
56
55
57
56
58
57
def reusable_waiter (loop , timeout ):
@@ -62,22 +61,22 @@ def reusable_waiter(loop, timeout):
62
61
63
62
waiter = reusable_waiter(event_loop, 10.0)
64
63
while some_condition:
65
- yield from waiter(some_future)
64
+ yield waiter(some_future)
66
65
"""
67
66
if timeout is not None :
68
67
deadline = loop .time () + timeout
69
68
else :
70
69
deadline = None
71
70
72
- @asyncio .coroutine
73
- def wait (future ):
71
+ async def wait (future ):
74
72
if deadline is not None :
75
73
new_timeout = max (deadline - loop .time (), 0 )
76
74
else :
77
75
new_timeout = None
78
- return (yield from asyncio .wait_for (future , new_timeout , loop = loop ))
76
+ yield asyncio .wait_for (future , new_timeout , loop = loop )
77
+ return
79
78
80
- return wait
79
+ yield wait
81
80
82
81
83
82
@contextlib .contextmanager
@@ -101,20 +100,18 @@ def __init__(self, *args, **kwargs):
101
100
def __aiter__ (self ):
102
101
return self
103
102
104
- @asyncio .coroutine
105
- def __anext__ (self ):
103
+ async def __anext__ (self ):
106
104
try :
107
- return ( yield from self ._get_next (None ) )
105
+ yield self ._get_next (None )
108
106
except ReqlCursorEmpty :
109
107
raise StopAsyncIteration
110
108
111
- @asyncio .coroutine
112
- def close (self ):
109
+ async def close (self ):
113
110
if self .error is None :
114
111
self .error = self ._empty_error ()
115
112
if self .conn .is_open ():
116
113
self .outstanding_requests += 1
117
- yield from self .conn ._parent ._stop (self )
114
+ yield self .conn ._parent ._stop (self )
118
115
119
116
def _extend (self , res_buf ):
120
117
Cursor ._extend (self , res_buf )
@@ -123,35 +120,35 @@ def _extend(self, res_buf):
123
120
124
121
# Convenience function so users know when they've hit the end of the cursor
125
122
# without having to catch an exception
126
- @asyncio .coroutine
127
- def fetch_next (self , wait = True ):
123
+ async def fetch_next (self , wait = True ):
128
124
timeout = Cursor ._wait_to_timeout (wait )
129
125
waiter = reusable_waiter (self .conn ._io_loop , timeout )
130
126
while len (self .items ) == 0 and self .error is None :
131
127
self ._maybe_fetch_batch ()
132
128
if self .error is not None :
133
129
raise self .error
134
130
with translate_timeout_errors ():
135
- yield from waiter (asyncio .shield (self .new_response ))
131
+ yield waiter (asyncio .shield (self .new_response ))
136
132
# If there is a (non-empty) error to be received, we return True, so the
137
133
# user will receive it on the next `next` call.
138
- return len (self .items ) != 0 or not isinstance (self .error , RqlCursorEmpty )
134
+ yield len (self .items ) != 0 or not isinstance (self .error , RqlCursorEmpty )
135
+ return
139
136
140
137
def _empty_error (self ):
141
138
# We do not have RqlCursorEmpty inherit from StopIteration as that interferes
142
139
# with mechanisms to return from a coroutine.
143
140
return RqlCursorEmpty ()
144
141
145
- @asyncio .coroutine
146
- def _get_next (self , timeout ):
142
+ async def _get_next (self , timeout ):
147
143
waiter = reusable_waiter (self .conn ._io_loop , timeout )
148
144
while len (self .items ) == 0 :
149
145
self ._maybe_fetch_batch ()
150
146
if self .error is not None :
151
147
raise self .error
152
148
with translate_timeout_errors ():
153
- yield from waiter (asyncio .shield (self .new_response ))
154
- return self .items .popleft ()
149
+ yield waiter (asyncio .shield (self .new_response ))
150
+ yield self .items .popleft ()
151
+ return
155
152
156
153
def _maybe_fetch_batch (self ):
157
154
if (
@@ -186,8 +183,7 @@ def client_address(self):
186
183
if self .is_open ():
187
184
return self ._streamwriter .get_extra_info ("sockname" )[0 ]
188
185
189
- @asyncio .coroutine
190
- def connect (self , timeout ):
186
+ async def connect (self , timeout ):
191
187
try :
192
188
ssl_context = None
193
189
if len (self ._parent .ssl ) > 0 :
@@ -199,7 +195,7 @@ def connect(self, timeout):
199
195
ssl_context .check_hostname = True # redundant with match_hostname
200
196
ssl_context .load_verify_locations (self ._parent .ssl ["ca_certs" ])
201
197
202
- self ._streamreader , self ._streamwriter = yield from asyncio .open_connection (
198
+ self ._streamreader , self ._streamwriter = yield asyncio .open_connection (
203
199
self ._parent .host ,
204
200
self ._parent .port ,
205
201
loop = self ._io_loop ,
@@ -230,23 +226,23 @@ def connect(self, timeout):
230
226
if request is not "" :
231
227
self ._streamwriter .write (request )
232
228
233
- response = yield from asyncio .wait_for (
229
+ response = yield asyncio .wait_for (
234
230
_read_until (self ._streamreader , b"\0 " ),
235
231
timeout ,
236
232
loop = self ._io_loop ,
237
233
)
238
234
response = response [:- 1 ]
239
235
except ReqlAuthError :
240
- yield from self .close ()
236
+ yield self .close ()
241
237
raise
242
238
except ReqlTimeoutError as err :
243
- yield from self .close ()
239
+ yield self .close ()
244
240
raise ReqlDriverError (
245
241
"Connection interrupted during handshake with %s:%s. Error: %s"
246
242
% (self ._parent .host , self ._parent .port , str (err ))
247
243
)
248
244
except Exception as err :
249
- yield from self .close ()
245
+ yield self .close ()
250
246
raise ReqlDriverError (
251
247
"Could not connect to %s:%s. Error: %s"
252
248
% (self ._parent .host , self ._parent .port , str (err ))
@@ -255,13 +251,13 @@ def connect(self, timeout):
255
251
# Start a parallel function to perform reads
256
252
# store a reference to it so it doesn't get destroyed
257
253
self ._reader_task = asyncio .ensure_future (self ._reader (), loop = self ._io_loop )
258
- return self ._parent
254
+ yield self ._parent
255
+ return
259
256
260
257
def is_open (self ):
261
258
return not (self ._closing or self ._streamreader .at_eof ())
262
259
263
- @asyncio .coroutine
264
- def close (self , noreply_wait = False , token = None , exception = None ):
260
+ async def close (self , noreply_wait = False , token = None , exception = None ):
265
261
self ._closing = True
266
262
if exception is not None :
267
263
err_message = "Connection is closed (%s)." % str (exception )
@@ -281,38 +277,37 @@ def close(self, noreply_wait=False, token=None, exception=None):
281
277
282
278
if noreply_wait :
283
279
noreply = Query (pQuery .NOREPLY_WAIT , token , None , None )
284
- yield from self .run_query (noreply , False )
280
+ yield self .run_query (noreply , False )
285
281
286
282
self ._streamwriter .close ()
287
283
# We must not wait for the _reader_task if we got an exception, because that
288
284
# means that we were called from it. Waiting would lead to a deadlock.
289
285
if self ._reader_task and exception is None :
290
- yield from self ._reader_task
286
+ yield self ._reader_task
291
287
292
- return None
288
+ return
293
289
294
- @asyncio .coroutine
295
- def run_query (self , query , noreply ):
290
+ async def run_query (self , query , noreply ):
296
291
self ._streamwriter .write (query .serialize (self ._parent ._get_json_encoder (query )))
297
292
if noreply :
298
- return None
293
+ return
299
294
300
295
response_future = asyncio .Future ()
301
296
self ._user_queries [query .token ] = (query , response_future )
302
- return (yield from response_future )
297
+ yield response_future
298
+ return
303
299
304
300
# The _reader coroutine runs in parallel, reading responses
305
301
# off of the socket and forwarding them to the appropriate Future or Cursor.
306
302
# This is shut down as a consequence of closing the stream, or an error in the
307
303
# socket/protocol from the server. Unexpected errors in this coroutine will
308
304
# close the ConnectionInstance and be passed to any open Futures or Cursors.
309
- @asyncio .coroutine
310
- def _reader (self ):
305
+ async def _reader (self ):
311
306
try :
312
307
while True :
313
- buf = yield from self ._streamreader .readexactly (12 )
308
+ buf = yield self ._streamreader .readexactly (12 )
314
309
(token , length ,) = struct .unpack ("<qL" , buf )
315
- buf = yield from self ._streamreader .readexactly (length )
310
+ buf = yield self ._streamreader .readexactly (length )
316
311
317
312
cursor = self ._cursor_cache .get (token )
318
313
if cursor is not None :
@@ -341,7 +336,7 @@ def _reader(self):
341
336
raise ReqlDriverError ("Unexpected response received." )
342
337
except Exception as ex :
343
338
if not self ._closing :
344
- yield from self .close (exception = ex )
339
+ yield self .close (exception = ex )
345
340
346
341
347
342
class Connection (ConnectionBase ):
@@ -354,30 +349,29 @@ def __init__(self, *args, **kwargs):
354
349
"Could not convert port %s to an integer." % self .port
355
350
)
356
351
357
- @asyncio .coroutine
358
- def __aenter__ (self ):
352
+ async def __aenter__ (self ):
359
353
return self
360
354
361
- @asyncio .coroutine
362
- def __aexit__ (self , exception_type , exception_val , traceback ):
363
- yield from self .close (False )
355
+ async def __aexit__ (self , exception_type , exception_val , traceback ):
356
+ yield self .close (False )
364
357
365
- @asyncio .coroutine
366
- def _stop (self , cursor ):
358
+ async def _stop (self , cursor ):
367
359
self .check_open ()
368
360
q = Query (pQuery .STOP , cursor .query .token , None , None )
369
- return (yield from self ._instance .run_query (q , True ))
361
+ yield self ._instance .run_query (q , True )
362
+ return
370
363
371
- @asyncio .coroutine
372
- def reconnect (self , noreply_wait = True , timeout = None ):
364
+ async def reconnect (self , noreply_wait = True , timeout = None ):
373
365
# We close before reconnect so reconnect doesn't try to close us
374
366
# and then fail to return the Future (this is a little awkward).
375
- yield from self .close (noreply_wait )
367
+ yield self .close (noreply_wait )
376
368
self ._instance = self ._conn_type (self , ** self ._child_kwargs )
377
- return (yield from self ._instance .connect (timeout ))
369
+ yield self ._instance .connect (timeout )
370
+ return
378
371
379
- @asyncio .coroutine
380
- def close (self , noreply_wait = True ):
372
+ async def close (self , noreply_wait = True ):
381
373
if self ._instance is None :
382
- return None
383
- return (yield from ConnectionBase .close (self , noreply_wait = noreply_wait ))
374
+ yield None
375
+ return
376
+ yield ConnectionBase .close (self , noreply_wait = noreply_wait )
377
+ return
0 commit comments