我正在编写一个脚本来执行一个进程列表,并将它们的所有输出连接到另一个进程的输入中。我已经将我的脚本压缩成一个测试用例,使用echo和cat作为实际进程的代理。在
#!/usr/bin/python
import os,subprocess
(pipeOut, pipeIn) = os.pipe()
catProcess = subprocess.Popen("/bin/cat", stdin = pipeOut)
for line in ["First line", "Last line"]:
subprocess.call(["/bin/echo",line], stdout = pipeIn)
os.close(pipeIn)
os.close(pipeOut)
catProcess.wait()
程序按预期工作,只是对catProcess.wait()
的调用挂起(可能是因为它仍在等待更多的输入)。将close_fds=True
传递给Popen
或{
有没有办法关闭catProcesses
的stdin,使其优雅地退出?或者有别的方法来写这个程序吗?在
将
close_fds=True
传递给catProcess
对我的系统有帮助。在不需要显式地创建管道:
相关问题 更多 >
编程相关推荐