2

子プロセス (C で記述) を時々生成し、バイナリ データを供給して応答を取得する必要がある Python アプリケーションを開発しています。子プロセスは、必要な場合にのみ生成され、1 つの要求のみを処理します。ここでのオプションは何ですか? stdin/stdout を使用しても安全ですか?

4

1 に答える 1

3
from subprocess import Popen,PIPE

# Example with output only
p = Popen(["echo", "This is a test"], stdout=PIPE)
out, err = p.communicate()
print out.rstrip()

# Example with input and output
p = Popen("./TestProgram", stdin=PIPE, stdout=PIPE)
out, err = p.communicate("This is the input\n")
print out.rstrip()

プログラムはTestProgramから 1 行を読み取りstdin、 に書き込みますstdout。出力にを追加し.rstrip()て、末尾の改行文字を削除しました。バイナリ データでは、おそらくこれを行いたくないでしょう。

于 2013-09-17T06:41:23.740 に答える