non blocking read()

Donn Cave donn at u.washington.edu
Wed Dec 1 17:32:08 EST 2004


In article <mailman.6995.1101939055.5135.python-list at python.org>,
 Gustavo Córdova Avila <gustavo.cordova at q-voz.com> wrote:

> David Bolen wrote:
> 
> >Jp Calderone <exarkun at divmod.com> writes:
> >
> >>    def nonBlockingReadAll(fileObj):
> >>        bytes = []
> >>        while True:
> >>            b = fileObj.read(1024)
> >>            bytes.append(b)
> >>            if len(b) < 1024:
> >>                break
> >>        return ''.join(bytes)
> >>
> >Wouldn't this still block if the input just happened to end at a
> >multiple of the read size (1024)?
> >
> >-- David
> >
> No, it'll read up to 1024 bytes or as much as it can, and
> then return an apropriatly sized string.

Depends.  I don't believe the original post mentioned
that the file is a pipe, socket or similar, but it's
kind of implied by the use of select() also mentioned.
It's also kind of implied by use of the term "block" -
disk files don't block.

If we are indeed talking about a pipe or something that
really can block, and you call fileobject.read(1024),
it will block until it gets 1024 bytes.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list