简体   繁体   English

python subprocess.stdout.readline 没有

[英]python subprocess.stdout.readline doesn't

I have a piece of python code that calls a binary via the subprocess module.我有一段通过subprocess模块调用二进制文件的 python 代码。 This binary reads data from stdin and outputs other data to stdout , which should then be collected by the python code.这个二进制文件从stdin读取数据并将其他数据输出到stdout ,然后应该由 python 代码收集。

Here's how I use this binary in a shell:下面是我在 shell 中使用这个二进制文件的方法:

$ ./check # start reading from stdin here
param_card_orig.dat
0.123809
42.821 0 0 42.821 
91.2246 0 0 -91.2246 
14.5521 -12.9194 -0.441262 6.68258 
33.8782 -4.04952 5.37574 -33.2029 
35.3909 22.0683 2.04142 27.5923 
50.2244 -5.09935 -6.9759 -49.4755 

with a return after each line (including the last), from which I get the output在每一行(包括最后一行)之后返回,从中我得到输出

1.53852538054399053E-004

which is again terminated by a newline.再次以换行符终止。 However, trying to put this in python code like this:但是,尝试将其放入 python 代码中,如下所示:

import subprocess

p = subprocess.Popen("./check", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write("param_card_orig.dat\n")
p.stdin.write("0.123809\n")
p.stdin.write("42.821 0 0 42.821 \n")
p.stdin.write("91.2246 0 0 -91.2246 \n")
p.stdin.write("14.5521 -12.9194 -0.441262 6.68258 \n")
p.stdin.write("33.8782 -4.04952 5.37574 -33.2029 \n")
p.stdin.write("35.3909 22.0683 2.04142 27.5923 \n")
p.stdin.write("50.2244 -5.09935 -6.9759 -49.4755 \n")

print("now waiting for input")

print(p.stdout.readline())

I receive exactly this:我收到的正是这个:

now waiting for input

The program is hanging, obviously waiting for some sort of communication to happen.该程序正在挂起,显然在等待某种通信发生。 This is partially intended - the check binary is supposed to stay active and wait for further input, returning the output of the calculation as soon as a further complete input is supplied - but since the first input is complete, I should have received the result of the calculation, which I obviously didn't, or else we would have seen the printout.这是部分意图 - check二进制文件应该保持活动状态并等待进一步的输入,一旦提供进一步的完整输入就返回计算的输出 - 但由于第一个输入完成,我应该收到结果计算,我显然没有,否则我们会看到打印输出。 When I break the execution manually with CTRL+C , it tells me当我用CTRL+C手动中断执行时,它告诉我

^CTraceback (most recent call last):
File "testpipe.py", line 15, in <module>
print(p.stdout.readline())

What's going on here?这里发生了什么? Is this a problem with the way I handle the pipes?这是我处理管道的方式的问题吗? Is this a problem with a missing EOF or missing newline?这是缺少EOF或缺少换行符的问题吗?

PS: I found this issue Python subprocess.stdout.readline() hang , which is possibly the same, but didn't receive an answer. PS:我发现这个问题Python subprocess.stdout.readline() hang ,这可能是一样的,但没有收到答案。

PPS: In case it matters, the python version is Python 2.6.6. PPS:以防万一,python 版本是 Python 2.6.6。

Have you tried using p.stdout.read() instead of readline() ?您是否尝试过使用p.stdout.read()而不是readline() It works for the following test code where invoker.py creates a pipe with invoked.py .它适用于在下面的测试代码invoker.py以创建一个管道invoked.py

invoked.py

#!/usr/bin/python
import sys

# Print a Hello notice
sys.stdout.write("I have been invoked!\n")

# Read the value coming from the pipe
value = sys.stdin.readline()

# Return a "processed" value
sys.stdout.write("New: " + value)

invoker.py

import subprocess

# Start pipe with other file
p = subprocess.Popen("./invoked.py", stdout=subprocess.PIPE, stdin=subprocess.PIPE)

# Send a value to the other file
p.stdin.write("Value!\n")

# Print the result coming from the other file
print(p.stdout.read())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM