我试图在这里遵循这样的回答:https://stackoverflow.com/a/5087695/343381发送多条命令是bash shell必须共享环境
我有一个单一的环境中需要执行多个命令的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()
永远不会返回?
注:
- 这是绝对必要的,我从运行巨蟒在同一环境中的多个的bash命令。因此,subprocess.communicate()没有帮助,因为它等待进程终止。
- 请注意,在我的真实测试案例中,它不是要执行的bash命令的静态列表。逻辑更加动态。我没有选择一次运行所有这些选项。
啊,现在看看;我怀疑你需要轮询管道以查看是否有任何内容。你也可以尝试只读第二个命令'stdin' pipe而不读取'stdout'。 – 2013-03-20 21:35:00