fcntl/ioctl and O_NONBLOCK
donn at oz.net
Sun Nov 28 07:45:26 CET 1999
Quoth hildeb at www.stahl.bau.tu-bs.de (Ralf Hildebrandt):
| Fine. My problem here is the following: I start a program (plot) and
| feed it with random input data until it crashes. Output is irrelevant
| to me, I need only know if it crashes or exit()'s.
| On the shell:
| % plot < t1 > /dev/null
| Segmentation fault.
| In my Python program, though: The program ran for a while and never
| returned. It didn't crash, it didn't exit, nothing. It just sat there
| status "sleep" (displayed top). It didn't consume cpu-time, either.
| I figured I should empty fromchild (since the command above generates
| 8MB of output) from time to time. Then I came across the select()
| This is also fixed now, but now the popen3 Object generates about 1 MB
| of output (which I read using select() and read()), and then it STILL
| doesn't return. No crash, no exit. And once more, in top it says
| "sleep" once more.
| Any ideas what's going on there?
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.
Donn Cave, donn at oz.net
More information about the Python-list