了解 Popen.communicate [英] Understanding Popen.communicate

查看:57
本文介绍了了解 Popen.communicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 1st.py 的脚本,它创建了一个 REPL(read-eval-print-loop):

print "要打印的东西"为真:r = raw_input()如果 r == 'n':打印退出"休息别的:打印继续"

然后我使用以下代码启动了 1st.py:

p = subprocess.Popen(["python","1st.py"], stdin=PIPE, stdout=PIPE)

然后尝试了这个:

print p.communicate()[0]

失败,提供此回溯:

回溯(最近一次调用最后一次): 中的文件1st.py",第 3 行r = raw_input()EOFError:读取一行时的EOF

你能解释一下这里发生了什么吗?当我使用 p.stdout.read() 时,它会永远挂起.

解决方案

.communicate() 写入输入(在这种情况下没有输入,所以它只是关闭子进程的 stdin 以指示子进程没有更多的输入),读取所有输出,并等待子进程退出.

raw_input() 在子进程中引发异常 EOFError(它需要数据但得到 EOF(无数据)).

p.stdout.read() 永远挂起,因为它试图在孩子等待输入(raw_input()) 导致死锁.

为了避免死锁,您需要异步读/写(例如,通过使用线程或选择)或确切知道读/写的时间和数量,例如:

from subprocess import PIPE, Popenp = Popen(["python", "-u", "1st.py"], stdin=PIPE, stdout=PIPE, bufsize=1)print p.stdout.readline(), # 读取第一行for i in range(10): # 重复几次以表明它有效print >>p.stdin, i # 写输入p.stdin.flush() # 在这种情况下不需要print p.stdout.readline(), # 读取输出print p.communicate("n\n")[0], # 通知孩子退出,# 读取剩余的输出,# 等待孩子退出

注意:如果读/写不同步,这是一个非常脆弱的代码;它会死锁.

注意块缓冲问题(这里使用"-u" 标志关闭标准输入的缓冲,标准输出在孩子).

bufsize=1 使管道行缓冲在父端.

I have a script named 1st.py which creates a REPL (read-eval-print-loop):

print "Something to print"
while True:
    r = raw_input()
    if r == 'n':
        print "exiting"
        break
    else:
        print "continuing"

I then launched 1st.py with the following code:

p = subprocess.Popen(["python","1st.py"], stdin=PIPE, stdout=PIPE)

And then tried this:

print p.communicate()[0]

It failed, providing this traceback:

Traceback (most recent call last):
  File "1st.py", line 3, in <module>
    r = raw_input()
EOFError: EOF when reading a line

Can you explain what is happening here please? When I use p.stdout.read(), it hangs forever.

解决方案

.communicate() writes input (there is no input in this case so it just closes subprocess' stdin to indicate to the subprocess that there is no more input), reads all output, and waits for the subprocess to exit.

The exception EOFError is raised in the child process by raw_input() (it expected data but got EOF (no data)).

p.stdout.read() hangs forever because it tries to read all output from the child at the same time as the child waits for input (raw_input()) that causes a deadlock.

To avoid the deadlock you need to read/write asynchronously (e.g., by using threads or select) or to know exactly when and how much to read/write, for example:

from subprocess import PIPE, Popen

p = Popen(["python", "-u", "1st.py"], stdin=PIPE, stdout=PIPE, bufsize=1)
print p.stdout.readline(), # read the first line
for i in range(10): # repeat several times to show that it works
    print >>p.stdin, i # write input
    p.stdin.flush() # not necessary in this case
    print p.stdout.readline(), # read output

print p.communicate("n\n")[0], # signal the child to exit,
                               # read the rest of the output, 
                               # wait for the child to exit

Note: it is a very fragile code if read/write are not in sync; it deadlocks.

Beware of block-buffering issue (here it is solved by using "-u" flag that turns off buffering for stdin, stdout in the child).

bufsize=1 makes the pipes line-buffered on the parent side.

这篇关于了解 Popen.communicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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