Nonblocking IO on popen pipe (isn't nonblocking)

Noah noah at noah.org
Thu Apr 11 05:03:47 EDT 2002


I'm having headaches with nonblocking IO on a pipe.
I opened a UNIX command through os.popen4.
Then I set O_NONBLOCK on the output stream of the pipe.
Finally I did a select() on the output pipe to wait for data.
If select returns I would try to read the data.
The problem is that select() is blocking. Select should block 
until some data is available on the pipe, so I assume that
select is never even seeing any data on the pipe.
I added a timeout to select() and then tried to read the
pipe, but it was empty (read() returned '').

One program that I am attempting to talk to via a pipe is ftp.
Any interactive program I open will block. 
If I try open a program that terminates like "ls" or "uname -a" 
then my script works as expected.

What am I doing wrong that is causing interactive programs to block?
Any help?  I'm using Python 2.1 (#1, Jul  2 2001, 23:39:12).
The following sample script runs, but blocks on my system.

#!/usr/bin/env python
import fcntl, FCNTL
import select
import os
# First I open FTP using os.popen4()
(child_stdin, child_stdout_and_stderr) = os.popen4 ("/usr/bin/ftp localhost")
# This is how I'm setting nonblocking IO on child_stdout_and_stderr
original_flags = fcntl.fcntl (child_stdout_and_stderr.fileno(), FCNTL.F_GETFL, 0)
flags = original_flags | FCNTL.O_NONBLOCK
fcntl.fcntl(child_stdout_and_stderr.fileno(), FCNTL.F_SETFL, flags)
# Then I select() on child_stdout_and_stderr to wait for data
(r,w,e) = select.select([child_stdout_and_stderr], [], [], None)
if len(r) > 0:
        print r[0].read()



Yours,
Noah





More information about the Python-list mailing list