Non-Blocking IO

M

mp

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
 
L

Lawrence D'Oliveiro

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

That's what's supposed to happen. That's telling you there are no bytes
currently available to be read.
 
H

Hendrik van Rooyen

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

This means there is no data available- its actually working!

- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top