从标准输入读取并将其转发到 Python 中的子进程 [英] Read from stdin AND forward it to a subprocess in Python

查看:22
本文介绍了从标准输入读取并将其转发到 Python 中的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个程序编写一个包装脚本,该程序可以选择接受来自 STDIN 的输入.我的脚本需要处理文件的每一行,但它还需要将 STDIN 转发到它正在包装的程序.在极简形式中,这看起来像这样:

I'm writing a wrapper script for a program that optionally accepts input from STDIN. My script needs to process each line of the file, but it also needs to forward STDIN to the program it is wrapping. In minimalist form, this looks something like this:

import subprocess
import sys

for line in sys.stdin:
    # Do something with each line
    pass

subprocess.call(['cat'])

请注意,我实际上并不是在尝试包装 cat,它只是作为一个示例来演示 STDIN 是否被正确转发.

Note that I'm not actually trying to wrap cat, it just serves as an example to demonstrate whether or not STDIN is being forwarded properly.

在上面的例子中,如果我注释掉 for 循环,它就可以正常工作.但是,如果我使用 for 循环运行它,则不会转发任何内容,因为我已经阅读到 STDIN 的末尾.我不能 seek(0) 到文件的开头,因为你不能在流上寻找.

With the example above, if I comment out the for-loop, it works properly. But if I run it with the for-loop, nothing gets forwarded because I've already read to the end of STDIN. I can't seek(0) to the start of the file because you can't seek on streams.

一种可能的解决方案是将整个文件读入内存:

One possible solution is to read the entire file into memory:

import subprocess
import sys

lines = sys.stdin.readlines()
for line in lines:
    # Do something with each line
    pass

p = subprocess.Popen(['cat'], stdin=subprocess.PIPE)
p.communicate(''.join(lines))

它可以工作,但内存效率不是很高.谁能想到更好的解决方案?也许是一种拆分或复制流的方法?

which works, but isn't very memory efficient. Can anyone think of a better solution? Perhaps a way to split or copy the stream?

附加限制:

  1. 子进程只能被调用一次.所以我不能一次读取一行,处理它,然后将它转发到子进程.
  2. 该解决方案必须适用于 Python 2.6

推荐答案

这对您有用吗?

#!/usr/bin/env python2
import subprocess
import sys

p = subprocess.Popen(['cat'], stdin = subprocess.PIPE)

line = sys.stdin.readline()

####################
# Insert work here #
####################

line = line.upper()

####################

p.communicate(line)

示例:

$ echo "hello world" | ./wrapper.py 
HELLO WORLD

这篇关于从标准输入读取并将其转发到 Python 中的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆