I\'m testing subprocesses pipelines with python. I\'m aware that I can do what the programs below do in python directly, but that\'s not the point. I just want to test the pipel
There are three main tricks to making pipes work as expected
Make sure each end of the pipe is used in a different thread/process (some of the examples near the top suffer from this problem).
explicitly close the unused end of the pipe in each process
deal with buffering by either disabling it (Python -u option), using pty's, or simply filling up the buffer with something that won't affect the data, ( maybe '\n', but whatever fits).
The examples in the Python "pipeline" module (I'm the author) fit your scenario exactly, and make the low-level steps fairly clear.
http://pypi.python.org/pypi/pipeline/
More recently, I used the subprocess module as part of a producer-processor-consumer-controller pattern:
http://www.darkarchive.org/w/Pub/PythonInteract
This example deals with buffered stdin without resorting to using a pty, and also illustrates which pipe ends should be closed where. I prefer processes to threading, but the principle is the same. Additionally, it illustrates synchronizing Queues to which feed the producer and collect output from the consumer, and how to shut them down cleanly (look out for the sentinels inserted into the queues). This pattern allows new input to be generated based on recent output, allowing for recursive discovery and processing.