如何将数据从一个管道馈送到三个不同的进程?
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()
如果我运行上面的代码,它将产生一个损坏的文件。这意味着管道使用者可能没有从吹笛者那里读取完整的输出。
这个可以正常工作,但速度要慢得多:
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()
有什么建议可以使第一个代码片段与第二个代码片段的工作方式相同吗?如果我得到第一种工作方法,该过程将在 1/3 的时间内完成。