1つのパイプから3つの異なるプロセスにデータをフィードするにはどうすればよいですか?
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()
最初のコードスニペットを2番目のコードスニペットと同じように機能させる方法についての提案はありますか?私が最初のアプローチを機能させると、プロセスは3分の1の時間で終了します。