[Python-ideas] Add function readbyte to asyncio.StreamReader

Jörn Heissler python-ideas-2018 at tutnicht.de
Sun Jul 22 07:03:40 EDT 2018


I'm implementing a protocol where I need to read individual bytes until
a condition is met (value & 0x80 == 0).

My current approach is: value = (await reader.readexactly(1))[0]

To speed this up, I propose that a new function is added to
asyncio.StreamReader: value = await reader.readbyte()

I duplicated readexactly and stripped out some parts. Below code appears
to work:

    async def readbyte(self):
        if self._exception is not None:
            raise self._exception

        while not self._buffer:
            if self._eof:
                raise EOFError()
            await self._wait_for_data('readbyte')

        data = self._buffer[0]
        del self._buffer[0]
        return data

For comparing the speed, I'm receiving a 50 MiB file byte-by-byte.

    readexactly: 42.43 seconds
    readbyte   : 22.05 seconds
    speedup    : 92.4%

    readexactly: 3.21 seconds
    readbyte   : 2.76 seconds
    speedup    : 16.3%


More information about the Python-ideas mailing list