我想使用相当于在perl中管道化一些shell命令的python。类似于open(PIPE,“command |”)的python版本。
我转到子流程模块并尝试执行以下操作:
p = subprocess.Popen("zgrep thingiwant largefile", shell=True, stdout=subprocess.PIPE)
这与我在perl中读取输出的方式相同,但它不会自行清除。当我离开口译员时
grep: writing output: Broken pipe
喷发了几百万次。我想我天真地希望这一切都能得到照顾,但事实并非如此。在p上调用terminate或kill似乎没有帮助。看看process表,我发现这会终止/bin/sh进程,但会留下子gzip来抱怨管道损坏。
正确的方法是什么?
打开管道后,可以使用命令输出:
p.stdout
:你是怎么执行这个程序的?
正确的方法是使用
有关详细信息,请参见文档。
问题是
pipe
已满。子进程停止,等待管道清空,但随后您的进程(Python解释器)退出,中断其管道末端(因此出现错误消息)。p.wait()
不会帮助您:p.communicate()
不会帮助您:p.stdout.read(num_bytes)
不会帮助您:这个故事的寓意是,对于大输出来说,
subprocess.PIPE
如果您的程序试图读取数据,那么它将注定失败(在我看来,您应该能够将p.stdout.read(bytes)
放入一个while p.returncode is None:
循环中,但上面的警告表明这可能会死锁)。文档建议用以下内容替换shell管道:
注意,
p2
直接从p1
获取其标准输入。这应该可以避免死锁,但是考虑到上面相互矛盾的警告,谁知道。无论如何,如果最后一部分对您不起作用(但是它应该应该),您可以尝试创建一个临时文件,将第一次调用中的所有数据写入该文件,然后将该临时文件用作下一个进程的输入。
相关问题 更多 >
编程相关推荐