Well, even if it is worth, i.e. your use case is not rare enough, I would suggest at least making it private, readexactly can call this specialised function if nbytes==1:

def _readbyte(self):
   ....

def readexactly(self, num):
   if num == 1:
      return self._readbyte()
  ... the rest stays the same..


But to be honest, you are probably better off managing the buffer yourself: Just call, e.g., stream.read(4096), it will return a buffer of up to 4k length, then you can iterate over the buffer byte by byte until the condition is met, repeat until the end of stream, or whatever.


On Sun, 22 Jul 2018 at 12:11, Jörn Heissler <python-ideas-2018@tutnicht.de> wrote:
Hello,

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]
        self._maybe_resume_transport()
        return data

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

cpython-3.7.0:
    readexactly: 42.43 seconds
    readbyte   : 22.05 seconds
    speedup    : 92.4%

pypy3-v6.0.0:
    readexactly: 3.21 seconds
    readbyte   : 2.76 seconds
    speedup    : 16.3%

Thanks
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


--
Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert