-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
streams.py
688 lines (538 loc) · 19.5 KB
/
streams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import asyncio
import collections
import traceback
from . import helpers
from .log import internal_logger
__all__ = (
'EMPTY_PAYLOAD', 'EofStream', 'StreamReader', 'DataQueue', 'ChunksQueue',
'FlowControlStreamReader',
'FlowControlDataQueue', 'FlowControlChunksQueue')
DEFAULT_LIMIT = 2 ** 16
class EofStream(Exception):
"""eof stream indication."""
if helpers.PY_35:
class AsyncStreamIterator:
def __init__(self, read_func):
self.read_func = read_func
def __aiter__(self):
return self
if not helpers.PY_352: # pragma: no cover
__aiter__ = asyncio.coroutine(__aiter__)
@asyncio.coroutine
def __anext__(self):
try:
rv = yield from self.read_func()
except EofStream:
raise StopAsyncIteration # NOQA
if rv == b'':
raise StopAsyncIteration # NOQA
return rv
class ChunkTupleAsyncStreamIterator(AsyncStreamIterator):
@asyncio.coroutine
def __anext__(self):
rv = yield from self.read_func()
if rv == (b'', False):
raise StopAsyncIteration # NOQA
return rv
class AsyncStreamReaderMixin:
if helpers.PY_35:
def __aiter__(self):
return AsyncStreamIterator(self.readline)
if not helpers.PY_352: # pragma: no cover
__aiter__ = asyncio.coroutine(__aiter__)
def iter_chunked(self, n):
"""Returns an asynchronous iterator that yields chunks of size n.
Python-3.5 available for Python 3.5+ only
"""
return AsyncStreamIterator(lambda: self.read(n))
def iter_any(self):
"""Returns an asynchronous iterator that yields all the available
data as soon as it is received
Python-3.5 available for Python 3.5+ only
"""
return AsyncStreamIterator(self.readany)
def iter_chunks(self):
"""Returns an asynchronous iterator that yields chunks of data
as they are received by the server. The yielded objects are tuples
of (bytes, bool) as returned by the StreamReader.readchunk method.
Python-3.5 available for Python 3.5+ only
"""
return ChunkTupleAsyncStreamIterator(self.readchunk)
class StreamReader(AsyncStreamReaderMixin):
"""An enhancement of asyncio.StreamReader.
Supports asynchronous iteration by line, chunk or as available::
async for line in reader:
...
async for chunk in reader.iter_chunked(1024):
...
async for slice in reader.iter_any():
...
"""
total_bytes = 0
def __init__(self, limit=DEFAULT_LIMIT, timer=None, loop=None):
self._limit = limit
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
self._size = 0
self._cursor = 0
self._http_chunk_splits = None
self._buffer = collections.deque()
self._buffer_offset = 0
self._eof = False
self._waiter = None
self._eof_waiter = None
self._exception = None
self._timer = timer
self._eof_callbacks = []
def __repr__(self):
info = [self.__class__.__name__]
if self._size:
info.append('%d bytes' % self._size)
if self._eof:
info.append('eof')
if self._limit != DEFAULT_LIMIT:
info.append('l=%d' % self._limit)
if self._waiter:
info.append('w=%r' % self._waiter)
if self._exception:
info.append('e=%r' % self._exception)
return '<%s>' % ' '.join(info)
def exception(self):
return self._exception
def set_exception(self, exc):
self._exception = exc
self._eof_callbacks.clear()
waiter = self._waiter
if waiter is not None:
self._waiter = None
if not waiter.done():
waiter.set_exception(exc)
waiter = self._eof_waiter
if waiter is not None:
self._eof_waiter = None
if not waiter.done():
waiter.set_exception(exc)
def on_eof(self, callback):
if self._eof:
try:
callback()
except Exception:
internal_logger.exception('Exception in eof callback')
else:
self._eof_callbacks.append(callback)
def feed_eof(self):
self._eof = True
waiter = self._waiter
if waiter is not None:
self._waiter = None
if not waiter.done():
waiter.set_result(True)
waiter = self._eof_waiter
if waiter is not None:
self._eof_waiter = None
if not waiter.done():
waiter.set_result(True)
for cb in self._eof_callbacks:
try:
cb()
except Exception:
internal_logger.exception('Exception in eof callback')
self._eof_callbacks.clear()
def is_eof(self):
"""Return True if 'feed_eof' was called."""
return self._eof
def at_eof(self):
"""Return True if the buffer is empty and 'feed_eof' was called."""
return self._eof and not self._buffer
@asyncio.coroutine
def wait_eof(self):
if self._eof:
return
assert self._eof_waiter is None
self._eof_waiter = helpers.create_future(self._loop)
try:
yield from self._eof_waiter
finally:
self._eof_waiter = None
def unread_data(self, data):
""" rollback reading some data from stream, inserting it to buffer head.
"""
if not data:
return
if self._buffer_offset:
self._buffer[0] = self._buffer[0][self._buffer_offset:]
self._buffer_offset = 0
self._size += len(data)
self._cursor -= len(data)
self._buffer.appendleft(data)
def feed_data(self, data):
assert not self._eof, 'feed_data after feed_eof'
if not data:
return
self._size += len(data)
self._buffer.append(data)
self.total_bytes += len(data)
waiter = self._waiter
if waiter is not None:
self._waiter = None
if not waiter.done():
waiter.set_result(False)
def begin_http_chunk_receiving(self):
if self._http_chunk_splits is None:
self._http_chunk_splits = []
def end_http_chunk_receiving(self):
if self._http_chunk_splits is None:
raise RuntimeError("Called end_chunk_receiving without calling "
"begin_chunk_receiving first")
if not self._http_chunk_splits or \
self._http_chunk_splits[-1] != self.total_bytes:
self._http_chunk_splits.append(self.total_bytes)
@asyncio.coroutine
def _wait(self, func_name):
# StreamReader uses a future to link the protocol feed_data() method
# to a read coroutine. Running two read coroutines at the same time
# would have an unexpected behaviour. It would not possible to know
# which coroutine would get the next data.
if self._waiter is not None:
raise RuntimeError('%s() called while another coroutine is '
'already waiting for incoming data' % func_name)
waiter = self._waiter = helpers.create_future(self._loop)
try:
if self._timer:
with self._timer:
yield from waiter
else:
yield from waiter
finally:
self._waiter = None
@asyncio.coroutine
def readline(self):
if self._exception is not None:
raise self._exception
line = []
line_size = 0
not_enough = True
while not_enough:
while self._buffer and not_enough:
offset = self._buffer_offset
ichar = self._buffer[0].find(b'\n', offset) + 1
# Read from current offset to found b'\n' or to the end.
data = self._read_nowait_chunk(ichar - offset if ichar else -1)
line.append(data)
line_size += len(data)
if ichar:
not_enough = False
if line_size > self._limit:
raise ValueError('Line is too long')
if self._eof:
break
if not_enough:
yield from self._wait('readline')
return b''.join(line)
@asyncio.coroutine
def read(self, n=-1):
if self._exception is not None:
raise self._exception
# migration problem; with DataQueue you have to catch
# EofStream exception, so common way is to run payload.read() inside
# infinite loop. what can cause real infinite loop with StreamReader
# lets keep this code one major release.
if __debug__:
if self._eof and not self._buffer:
self._eof_counter = getattr(self, '_eof_counter', 0) + 1
if self._eof_counter > 5:
stack = traceback.format_stack()
internal_logger.warning(
'Multiple access to StreamReader in eof state, '
'might be infinite loop: \n%s', stack)
if not n:
return b''
if n < 0:
# This used to just loop creating a new waiter hoping to
# collect everything in self._buffer, but that would
# deadlock if the subprocess sends more than self.limit
# bytes. So just call self.readany() until EOF.
blocks = []
while True:
block = yield from self.readany()
if not block:
break
blocks.append(block)
return b''.join(blocks)
if not self._buffer and not self._eof:
yield from self._wait('read')
return self._read_nowait(n)
@asyncio.coroutine
def readany(self):
if self._exception is not None:
raise self._exception
if not self._buffer and not self._eof:
yield from self._wait('readany')
return self._read_nowait(-1)
@asyncio.coroutine
def readchunk(self):
"""Returns a tuple of (data, end_of_http_chunk). When chunked transfer
encoding is used, end_of_http_chunk is a boolean indicating if the end
of the data corresponds to the end of a HTTP chunk , otherwise it is
always False.
"""
if self._exception is not None:
raise self._exception
if not self._buffer and not self._eof:
if (self._http_chunk_splits and
self._cursor == self._http_chunk_splits[0]):
# end of http chunk without available data
self._http_chunk_splits = self._http_chunk_splits[1:]
return (b"", True)
yield from self._wait('readchunk')
if not self._buffer:
# end of file
return (b"", False)
elif self._http_chunk_splits is not None:
while self._http_chunk_splits:
pos = self._http_chunk_splits[0]
self._http_chunk_splits = self._http_chunk_splits[1:]
if pos > self._cursor:
return (self._read_nowait(pos-self._cursor), True)
return (self._read_nowait(-1), False)
else:
return (self._read_nowait_chunk(-1), False)
@asyncio.coroutine
def readexactly(self, n):
if self._exception is not None:
raise self._exception
blocks = []
while n > 0:
block = yield from self.read(n)
if not block:
partial = b''.join(blocks)
raise asyncio.streams.IncompleteReadError(
partial, len(partial) + n)
blocks.append(block)
n -= len(block)
return b''.join(blocks)
def read_nowait(self, n=-1):
# default was changed to be consistent with .read(-1)
#
# I believe the most users don't know about the method and
# they are not affected.
if self._exception is not None:
raise self._exception
if self._waiter and not self._waiter.done():
raise RuntimeError(
'Called while some coroutine is waiting for incoming data.')
return self._read_nowait(n)
def _read_nowait_chunk(self, n):
first_buffer = self._buffer[0]
offset = self._buffer_offset
if n != -1 and len(first_buffer) - offset > n:
data = first_buffer[offset:offset + n]
self._buffer_offset += n
elif offset:
self._buffer.popleft()
data = first_buffer[offset:]
self._buffer_offset = 0
else:
data = self._buffer.popleft()
self._size -= len(data)
self._cursor += len(data)
return data
def _read_nowait(self, n):
chunks = []
while self._buffer:
chunk = self._read_nowait_chunk(n)
chunks.append(chunk)
if n != -1:
n -= len(chunk)
if n == 0:
break
return b''.join(chunks) if chunks else b''
class EmptyStreamReader(AsyncStreamReaderMixin):
def exception(self):
return None
def set_exception(self, exc):
pass
def on_eof(self, callback):
try:
callback()
except Exception:
internal_logger.exception('Exception in eof callback')
def feed_eof(self):
pass
def is_eof(self):
return True
def at_eof(self):
return True
@asyncio.coroutine
def wait_eof(self):
return
def feed_data(self, data):
pass
@asyncio.coroutine
def readline(self):
return b''
@asyncio.coroutine
def read(self, n=-1):
return b''
@asyncio.coroutine
def readany(self):
return b''
@asyncio.coroutine
def readchunk(self):
return (b'', False)
@asyncio.coroutine
def readexactly(self, n):
raise asyncio.streams.IncompleteReadError(b'', n)
def read_nowait(self):
return b''
EMPTY_PAYLOAD = EmptyStreamReader()
class DataQueue:
"""DataQueue is a general-purpose blocking queue with one reader."""
def __init__(self, *, loop=None):
self._loop = loop
self._eof = False
self._waiter = None
self._exception = None
self._size = 0
self._buffer = collections.deque()
def __len__(self):
return len(self._buffer)
def is_eof(self):
return self._eof
def at_eof(self):
return self._eof and not self._buffer
def exception(self):
return self._exception
def set_exception(self, exc):
self._eof = True
self._exception = exc
waiter = self._waiter
if waiter is not None:
self._waiter = None
if not waiter.done():
waiter.set_exception(exc)
def feed_data(self, data, size=0):
self._size += size
self._buffer.append((data, size))
waiter = self._waiter
if waiter is not None:
self._waiter = None
if not waiter.cancelled():
waiter.set_result(True)
def feed_eof(self):
self._eof = True
waiter = self._waiter
if waiter is not None:
self._waiter = None
if not waiter.cancelled():
waiter.set_result(False)
@asyncio.coroutine
def read(self):
if not self._buffer and not self._eof:
assert not self._waiter
self._waiter = helpers.create_future(self._loop)
try:
yield from self._waiter
except (asyncio.CancelledError, asyncio.TimeoutError):
self._waiter = None
raise
if self._buffer:
data, size = self._buffer.popleft()
self._size -= size
return data
else:
if self._exception is not None:
raise self._exception
else:
raise EofStream
if helpers.PY_35:
def __aiter__(self):
return AsyncStreamIterator(self.read)
if not helpers.PY_352: # pragma: no cover
__aiter__ = asyncio.coroutine(__aiter__)
class ChunksQueue(DataQueue):
"""Like a :class:`DataQueue`, but for binary chunked data transfer."""
@asyncio.coroutine
def read(self):
try:
return (yield from super().read())
except EofStream:
return b''
readany = read
class FlowControlStreamReader(StreamReader):
def __init__(self, protocol, buffer_limit=DEFAULT_LIMIT, *args, **kwargs):
super().__init__(*args, **kwargs)
self._protocol = protocol
self._b_limit = buffer_limit * 2
def feed_data(self, data, size=0):
super().feed_data(data)
if self._size > self._b_limit and not self._protocol._reading_paused:
self._protocol.pause_reading()
@asyncio.coroutine
def read(self, n=-1):
try:
return (yield from super().read(n))
finally:
if self._size < self._b_limit and self._protocol._reading_paused:
self._protocol.resume_reading()
@asyncio.coroutine
def readline(self):
try:
return (yield from super().readline())
finally:
if self._size < self._b_limit and self._protocol._reading_paused:
self._protocol.resume_reading()
@asyncio.coroutine
def readany(self):
try:
return (yield from super().readany())
finally:
if self._size < self._b_limit and self._protocol._reading_paused:
self._protocol.resume_reading()
@asyncio.coroutine
def readchunk(self):
try:
return (yield from super().readchunk())
finally:
if self._size < self._b_limit and self._protocol._reading_paused:
self._protocol.resume_reading()
@asyncio.coroutine
def readexactly(self, n):
try:
return (yield from super().readexactly(n))
finally:
if self._size < self._b_limit and self._protocol._reading_paused:
self._protocol.resume_reading()
def read_nowait(self, n=-1):
try:
return super().read_nowait(n)
finally:
if self._size < self._b_limit and self._protocol._reading_paused:
self._protocol.resume_reading()
class FlowControlDataQueue(DataQueue):
"""FlowControlDataQueue resumes and pauses an underlying stream.
It is a destination for parsed data."""
def __init__(self, protocol, *, limit=DEFAULT_LIMIT, loop=None):
super().__init__(loop=loop)
self._protocol = protocol
self._limit = limit * 2
def feed_data(self, data, size):
super().feed_data(data, size)
if self._size > self._limit and not self._protocol._reading_paused:
self._protocol.pause_reading()
@asyncio.coroutine
def read(self):
try:
return (yield from super().read())
finally:
if self._size < self._limit and self._protocol._reading_paused:
self._protocol.resume_reading()
class FlowControlChunksQueue(FlowControlDataQueue):
@asyncio.coroutine
def read(self):
try:
return (yield from super().read())
except EofStream:
return b''
readany = read