non blocking read()

Jp Calderone exarkun at divmod.com
Wed Dec 1 21:06:29 CET 2004


On Wed, 01 Dec 2004 19:39:45 +0100, Uwe Mayer <merkosh at hadiko.de> wrote:
>Hi,
> 
> I use select() to wait for a file object (stdin) to become readable. In that
> situation I wanted to read everything available from stdin and return to
> the select statement to wait for more.
> 
> However, the file object's read method blocks if the number of bytes is 0 or
> negative. 
> 
> Is there no way to read everything a channel's got currently got without
> blocking?

    def nonBlockingReadAll(fileObj):
        bytes = []
        while True:
            b = fileObj.read(1024)
            bytes.append(b)
            if len(b) < 1024:
                break
        return ''.join(bytes)

  Jp



More information about the Python-list mailing list