Reputation: 44361
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)
Upvotes: 2
Views: 351
Reputation: 63709
You may be thinking of coroutines. Check out this very interesting presentation by David Beazley.
Upvotes: 1
Reputation: 236004
You can use the pipes module:
The pipes module defines a class to abstract the concept of a pipeline — a sequence of converters from one file to another.
Sure, the syntax won't be the same as a shell pipe, but why reinvent the wheel?
Upvotes: 2