ここでこの回答に従おうとしています: https://stackoverflow.com/a/5087695/343381
単一の環境内で複数の bash コマンドを実行する必要があります。私のテストケースは簡単です:
import subprocess
cmd = subprocess.Popen(['bash'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# Write the first command
command = "export greeting=hello\n"
cmd.stdin.write(command)
cmd.stdin.flush() # Must include this to ensure data is passed to child process
result = cmd.stdout.read()
print result
# Write the second command
command = "echo $greeting world\n"
cmd.stdin.write(command)
cmd.stdin.flush() # Must include this to ensure data is passed to child process
result = cmd.stdout.read()
print result
(参照された回答に基づいて)私が予想したことは、「hello world」が表示されることです。実際に起こることは、最初の にハングアップし、cmd.stdout.read()
二度と戻らないということです。
cmd.stdout.read()
二度と戻らない理由を誰か説明できますか?
ノート:
- 同じ環境内で python から複数の bash コマンドを実行することが絶対に不可欠です。したがって、 subprocess.communicate() はプロセスが終了するのを待つため、役に立ちません。
- 私の実際のテスト ケースでは、実行する bash コマンドの静的リストではないことに注意してください。ロジックはより動的です。それらすべてを一度に実行するオプションはありません。