[Python-Dev] How io.IOBase.readline() should behave when used on non-blocking obj and no data available?

Paul Sokolovsky pmiscml at gmail.com
Wed Oct 22 22:36:35 CEST 2014


On Thu, 16 Oct 2014 07:50:02 -0700
Guido van Rossum <guido at python.org> wrote:

> > > So, how readline() should behave in this case, and can that be
> > > specified in the Library Reference?
> >
> > Well, the problem is that it's not obvious how to implement such
> > methods in a non-blocking context.
> >
> > Let's says some data is received but there isn't a complete line.
> > Should readline() return just that data (an incomplete line)? That
> > breaks the API's contract. Should readline() buffer the incomplete
> > line and keep it for the next readline() call? But then the
> > internal buffer becomes unbounded: perhaps there is no new line in
> > the next 4GB of incoming data...
> >
> > And besides, raw I/O objects *shouldn't* have an internal buffer.
> > That's the role of the buffered I/O layer.
> >

> (if any). If the underlying stream returns None, I think it makes
> sense for readline() to return None too -- attempting to read more
> will just turn into a busy-wait loop, and that's the opposite of what
> should happen.
> (Alternatively, we could raise BlockingIOError, which is that the OS
> level read() raises if there's no data immediately available on a
> non-blocking FD; but it seems that streams have already gotten a

Yes, that's the choices I had in mind - either return None, or raise
an exception. Thanks for confirming that None is a better choice here,
that's what I implemented in MicroPython. Thanks for other points
and commentary too.

Best regards,
 Paul                          mailto:pmiscml at gmail.com

More information about the Python-Dev mailing list