Non-Blocking IO

mp mailpitches at email.com
Fri Aug 31 22:42:55 EDT 2007


I'm trying to use popen2 to call a program and then write and read
data from the program using a Python script. Unfortunately, my calls
to read block (I need non-blocking IO), and all the workarounds I've
seen online don't work. Here is my most promising solution and how it
breaks:

Source of solution: http://mail.python.org/pipermail/python-dev/2005-March/052263.html


def
setblocking(fd,flag):
    " set/clear blocking
mode"
    # get the file's current flag
settings
    fl = fcntl.fcntl(fd,
fcntl.F_GETFL)
    if
flag:
        # clear non-blocking mode from
flags
        fl = fl &
~os.O_NONBLOCK
 
else:
        # set non-blocking mode from
flags
        fl = fl |
os.O_NONBLOCK
    # update the file's
flags
    fcntl.fcntl(fd, fcntl.F_SETFL,
fl)


def
try3():
    fin,fout=
os.popen2("echo.py")
 
setblocking(fout.fileno(),False)
 
os.write(fin.fileno(),'blah')
 
fin.flush()
    print os.read(fout.fileno(),256)

Calling try3() yields the error:
  File "./test.py", line 54, in try3
    print os.read(fout.fileno(),256)
OSError: [Errno 35] Resource temporarily unavailable



If anyone could help me accomplish this I'd be extremely grateful.
Thanks!
MP




More information about the Python-list mailing list