How can I feed data from one pipe to three different processes?
nulfp = open(os.devnull, "w")
piper = Popen([
"come command",
"some params"
], stdout = PIPE, stderr = nulfp.fileno())
pipe_consumer_1 = Popen([
"come command",
"some params"
], stdin = piper.stdout, stderr = nulfp.fileno())
pipe_consumer_2 = Popen([
"come command",
"some params"
], stdin = piper.stdout, stderr = nulfp.fileno())
pipe_consumer_3 = Popen([
"come command",
"some params"
], stdin = piper.stdout, stderr = nulfp.fileno())
pipe_consumer_1.communicate()
pipe_consumer_2.communicate()
pipe_consumer_3.communicate()
piper.communicate()
If I run the code above, it will produce a corrupted file. Meaning that pipe consumers are probably not reading the full output from the piper.
This one works properly but is much slower:
nulfp = open(os.devnull, "w")
piper_1 = Popen([
"come command",
"some params"
], stdout = PIPE, stderr = nulfp.fileno())
piper_2 = Popen([
"come command",
"some params"
], stdout = PIPE, stderr = nulfp.fileno())
piper_3 = Popen([
"come command",
"some params"
], stdout = PIPE, stderr = nulfp.fileno())
pipe_consumer_1 = Popen([
"come command",
"some params"
], stdin = piper_1.stdout, stderr = nulfp.fileno())
pipe_consumer_2 = Popen([
"come command",
"some params"
], stdin = piper_2.stdout, stderr = nulfp.fileno())
pipe_consumer_3 = Popen([
"come command",
"some params"
], stdin = piper_3.stdout, stderr = nulfp.fileno())
pipe_consumer_1.communicate()
pipe_consumer_2.communicate()
pipe_consumer_3.communicate()
piper_1.communicate()
piper_2.communicate()
piper_3.communicate()
Any suggestions how to make the first code snippet work the same way as the second one? If I get the first approach to work, the process would finish in 1/3 of time.
This only uses a single byte ‘block’ but you get the idea.
When testing, the consumer output files matches the input file.
Edit:
Here is reading from a process with 1K blocks.