Non blocking IO

Graeme Winter g.winter at dl.ac.uk
Mon Feb 17 08:25:49 EST 2003


Graeme Winter wrote:
> Howdy all,
> 
> I have opened a pipe to a program using popen2.Popen3, and I am readling 
> the output using readline(). However, this program has a small tendency 
> to get stuck, and I need a method for timing this out, so that if I 
> don't receive a line for say 10 seconds I raise a RuntimeError. However, 
> I have attempted to implement such using select.select() and it doesn't 
> work, because the readline() blocks - any solutions out there?
> 
> I have (just) tried writing a little doufer in C to wrap fcntl (I know 
> there is a fcntl module) and I get:
> 
> Traceback (most recent call last):
>   File "test_blocking.py", line 43, in ?
>     blocking_test(5)
>   File "test_blocking.py", line 33, in blocking_test
>     line = stdout.readline()
> IOError: [Errno 11] Resource temporarily unavailable
> 
> Which I guess means that readline() doesn't like non blocking fd's....
> 
> What do people do here?
> 
> Cheers,
> 
> Graeme
> 

Hello Again,

I have rewritten the example entirely in Python - and I still get the 
same effect:

import popen2, select, sys, os, fcntl

def blocking_test(timeout = 5):
     pipe = popen2.Popen3("./test_blocking_executable", 0, 0)
     stdin = pipe.tochild
     stdout = pipe.fromchild

     stdin.write("10\n")
     stdin.close()

     fcntl.fcntl(stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
     # read the output
     while 1:
         channel = select.select([], [stdout], [], timeout)
         if channel[1] == []:
             # we have timed out
             raise RuntimeError, "Timed out"
         else:
             line = stdout.readline()
             if not line:
                 break
             sys.stdout.write(">" + line)
             sys.stdout.flush()

     stdout.close()

if __name__ == "__main__":
     blocking_test(5)

(get the same error message when I am finished)

So am I right to be thinking that readline() can't cope with non 
blocking IO? If so, is it worth writing something to handle this?

Cheers,

Graeme





More information about the Python-list mailing list