python,子进程:从子进程读取输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3804727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:49:07  来源:igfitidea点击:

python, subprocess: reading output from subprocess

pythonsubprocessstdout

提问by gruszczy

I have following script:

我有以下脚本:

#!/usr/bin/python

while True:
    x = raw_input()
    print x[::-1]

I am calling it from ipython:

我从ipython以下位置调用它:

In [5]: p = Popen('./script.py', stdin=PIPE)

In [6]: p.stdin.write('abc\n')
cba

and it works fine.

它工作正常。

However, when I do this:

但是,当我这样做时:

In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)

In [8]: p.stdin.write('abc\n')

In [9]: p.stdout.read()

the interpreter hangs. What am I doing wrong? I would like to be able to both write and read from another process multiple times, to pass some tasks to this process. What do I need to do differently?

口译员挂起。我究竟做错了什么?我希望能够多次写入和读取另一个进程,将一些任务传递给这个进程。我需要做什么不同的事情?

EDIT 1

编辑 1

If I use communicate, I get this:

如果我使用communicate,我会得到这个:

In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)

In [8]: p.communicate('abc\n')
Traceback (most recent call last):
  File "./script.py", line 4, in <module>
    x = raw_input()
EOFError: EOF when reading a line
Out[8]: ('cba\n', None)

EDIT 2

编辑 2

I tried flushing:

我试过冲洗:

#!/usr/bin/python

import sys

while True:
        x = raw_input()
        print x[::-1]
        sys.stdout.flush()

and here:

和这里:

In [5]: from subprocess import PIPE, Popen

In [6]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)

In [7]: p.stdin.write('abc')

In [8]: p.stdin.flush()

In [9]: p.stdout.read()

but it hangs again.

但它再次挂起。

采纳答案by Daniel Stutzbach

I believe there are two problems at work here:

我认为这里有两个问题在起作用:

1) Your parent script calls p.stdout.read(), which will read all data until end-of-file. However, your child script runs in an infinite loop so end-of-file will never happen. Probably you want p.stdout.readline()?

1) 您的父脚本调用p.stdout.read(),它将读取所有数据,直到文件结束。但是,您的子脚本在无限循环中运行,因此文件结尾永远不会发生。大概你想要p.stdout.readline()

2) In interactive mode, most programs do buffer only one line at a time. When run from another program, they buffer much more. The buffering improves efficiency in many cases, but causes problems when two programs need to communicate interactively.

2) 在交互模式下,大多数程序一次只缓冲一行。当从另一个程序运行时,它们会缓冲更多。缓冲在很多情况下提高了效率,但是当两个程序需要交互通信时会导致问题。

After p.stdin.write('abc\n')add:

p.stdin.write('abc\n')加:

p.stdin.flush()

In your subprocess script, after print x[::-1]add the following within the loop:

在您的子流程脚本中,在print x[::-1]循环中添加以下内容后:

sys.stdout.flush()

(and import sysat the top)

(并import sys在顶部)

回答by ssokolow

You're probably tripping over Python's output buffering. Here's what python --helphas to say about it.

您可能会被 Python 的输出缓冲绊倒。下面是python --help关于它的内容。

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
         see man page for details on internal buffering relating to '-u'

回答by David Narayan

Use communicate()instead of .stdout.read().

使用communicate()代替.stdout.read()

Example:

例子:

from subprocess import Popen, PIPE
p = Popen('./script.py', stdin=PIPE, stdout=PIPE, stderr=PIPE)
input = 'abc\n'
stdout, stderr = p.communicate(input)

This recommendation comes from the Popen objectssection in the subprocess documentation:

此建议来自子流程文档中Popen 对象部分:

Warning: Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

警告:使用communication() 而不是.stdin.write、.stdout.read 或.stderr.read 来避免由于任何其他操作系统管道缓冲区填满并阻塞子进程而导致的死锁。

回答by Thud Foo

When you are through writing to p.stdin, close it: p.stdin.close()

当你写完 p.stdin 时,关闭它: p.stdin.close()

回答by JoshD

The subprocess method check_outputcan be useful for this:

子流程方法check_output对此很有用:

output = subprocess.check_output('./script.py')

output = subprocess.check_output('./script.py')

And output will be the stdout from the process. If you need stderr, too:

输出将是该过程的标准输出。如果您也需要 stderr:

output = subprocess.check_output('./script.py', stderr=subprocess.STDOUT)

output = subprocess.check_output('./script.py', stderr=subprocess.STDOUT)

Because you avoid managing pipes directly, it may circumvent your issue.

因为您避免直接管理管道,所以它可能会规避您的问题。

回答by jfs

If you'd like to pass several lines to script.pythen you need to read/write simultaneously:

如果你想传递几行,script.py那么你需要同时读/写:

#!/usr/bin/env python
import sys
from subprocess import PIPE, Popen
from threading  import Thread

def print_output(out, ntrim=80):
    for line in out:
        print len(line)
        if len(line) > ntrim: # truncate long output
            line = line[:ntrim-2]+'..'
        print line.rstrip() 


if __name__=="__main__":
    p = Popen(['python', 'script.py'], stdin=PIPE, stdout=PIPE)
    Thread(target=print_output, args=(p.stdout,)).start()
    for s in ['abc', 'def', 'ab'*10**7, 'ghi']:
        print >>p.stdin, s
    p.stdin.close()
    sys.exit(p.wait()) #NOTE: read http://docs.python.org/library/subprocess.html#subprocess.Popen.wait

Output:

输出:

4
cba
4
fed
20000001
bababababababababababababababababababababababababababababababababababababababa..
4
ihg

Where script.py:

哪里script.py

#!/usr/bin/env python
"""Print reverse lines."""
while True:
    try: x = raw_input()
    except EOFError:
        break # no more input
    else:
        print x[::-1]

Or

或者

#!/usr/bin/env python
"""Print reverse lines."""
import sys

for line in sys.stdin:
    print line.rstrip()[::-1]

Or

或者

#!/usr/bin/env python
"""Print reverse lines."""
import fileinput

for line in fileinput.input(): # accept files specified as command line arguments
    print line.rstrip()[::-1]