fcntl/ioctl and O_NONBLOCK

Ralf Hildebrandt hildeb at www.stahl.bau.tu-bs.de
Mon Nov 29 15:42:32 CET 1999


On 28 Nov 1999 06:45:26 GMT, Donn Cave <donn at oz.net> wrote:

>Yes - only a guess, but it bites everyone at least once when trying
>to talk to another process via pipes.  After each write(), where your
>Python program is writing to the ``stdin'' of the plot process, add
>a flush(), i.e. stdin.flush().
>
>Python file objects use C buffered I/O, and when output is a pipe
>device it's block buffered.  This means your data doesn't go directly
>to the pipe, but into a buffer, and the pipe doesn't see it until a
>certain amount has accumulated.  The flush() forces all the buffered
>data to be written immediately anyway.  You can also control this
>by the buffer size parameter to the function that creates the file
>object, or just avoid a file object altogether and write directly
>to the pipe file descriptor.

Meanwhile I found out:

1) I use select() to see if there's s.th. to read/write
2) I use os.read(fd, buffersize) to read and make the fd non-blocking using 

        fcntl.fcntl(stdout.fileno(), FCNTL.F_SETFD, FCNTL.O_NDELAY)
        fcntl.fcntl(stderr.fileno(), FCNTL.F_SETFD, FCNTL.O_NDELAY)

The problem was that the normal commands std(out|err).read() are
blocking, because they don't encounter an EOF. They just keep reading.





More information about the Python-list mailing list