non blocking read()

Jp Calderone exarkun at divmod.com
Wed Dec 1 23:07:10 CET 2004


On 01 Dec 2004 15:55:18 -0500, David Bolen <db3l at fitlinxx.com> 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)?

  Only if the file has not been put into non-blocking mode.  But the function as given is wrong, I left out the exception handling in the case you mention.  Rather than blocking forever, fileObj.read(1024) will raise IOError (EAGAIN) if the input happened to be a multiple of 1024 bytes long.  Here is the corrected version:

    def nonBlockingReadAll(fileObj):
        bytes = []
        while True:
            try:
                b = fileObj.read(1024)
            except IOError, e:
                if e.args[0] == errno.EAGAIN:
                    break
                raise
            bytes.append(b)
            if len(b) < 1024:
                break
        return ''.join(bytes)

  Arguably, there is no longer any point to checking the length of b in this version, since at worst you will break on the next iteration of the loop.  It doesn't hurt, though.

  Jp



More information about the Python-list mailing list