I would like to implement in python something like this:
def producer():
while True:
sys.stdout.write("this is my data\n")
def consumer():
while True:
data = sys.stdin.read()
print data
producer | consumer
The pipe actually needs to create two processes, connect stdout and stdin, and run them until both terminate.
Is there syntactical support for that in python, as the shell has, or do I need to recurse to the Popen object?
What is the simplest implementation in terms of Popen?
Could somebody offer a generic class which can be used to implement this piping pattern? The class would have a signature similar to this:
Class Pipe:
def __init__(self, process1, process2, ...):
So that, in my case, could be used as follows:
mypipe = Pipe(producer, consumer)
You may be thinking of coroutines. Check out this very interesting presentation by David Beazley.