cpython: b3ef922e6f26 (original) (raw)
Mercurial > cpython
changeset 104305:b3ef922e6f26 3.5
Issue #28370: Speedup asyncio.StreamReader.readexactly Patch by Коренберг Марк. [#28370]
Yury Selivanov yury@magic.io | |
---|---|
date | Wed, 05 Oct 2016 18:01:12 -0400 |
parents | 2b502f624753 |
children | b76553de3a29 c8e71ddf1db5 |
files | Lib/asyncio/streams.py Misc/NEWS |
diffstat | 2 files changed, 19 insertions(+), 18 deletions(-)[+] [-] Lib/asyncio/streams.py 34 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -448,6 +448,7 @@ class StreamReader: assert not self._eof, '_wait_for_data after EOF' # Waiting for data while paused will make deadlock, so prevent it.
# This is essential for readexactly(n) for case when n > self._limit.[](#l1.7) if self._paused:[](#l1.8) self._paused = False[](#l1.9) self._transport.resume_reading()[](#l1.10)
@@ -658,25 +659,22 @@ class StreamReader: if n == 0: return b''
# There used to be "optimized" code here. It created its own[](#l1.15)
# Future and waited until self._buffer had at least the n[](#l1.16)
# bytes, then called read(n). Unfortunately, this could pause[](#l1.17)
# the transport if the argument was larger than the pause[](#l1.18)
# limit (which is twice self._limit). So now we just read()[](#l1.19)
# into a local buffer.[](#l1.20)
while len(self._buffer) < n:[](#l1.21)
if self._eof:[](#l1.22)
incomplete = bytes(self._buffer)[](#l1.23)
self._buffer.clear()[](#l1.24)
raise IncompleteReadError(incomplete, n)[](#l1.25)
yield from self._wait_for_data('readexactly')[](#l1.27)
blocks = [][](#l1.29)
while n > 0:[](#l1.30)
block = yield from self.read(n)[](#l1.31)
if not block:[](#l1.32)
partial = b''.join(blocks)[](#l1.33)
raise IncompleteReadError(partial, len(partial) + n)[](#l1.34)
blocks.append(block)[](#l1.35)
n -= len(block)[](#l1.36)
assert n == 0[](#l1.38)
return b''.join(blocks)[](#l1.40)
if len(self._buffer) == n:[](#l1.41)
data = bytes(self._buffer)[](#l1.42)
self._buffer.clear()[](#l1.43)
else:[](#l1.44)
data = bytes(self._buffer[:n])[](#l1.45)
del self._buffer[:n][](#l1.46)
self._maybe_resume_transport()[](#l1.47)
return data[](#l1.48)