Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Popen-在不冻结或关闭标准输入的情况下读取标准输出和标准输出_Python_Python 3.x_Popen - Fatal编程技术网

Python Popen-在不冻结或关闭标准输入的情况下读取标准输出和标准输出

Python Popen-在不冻结或关闭标准输入的情况下读取标准输出和标准输出,python,python-3.x,popen,Python,Python 3.x,Popen,我正在用Python编写一个与jar文件交互的脚本,jar文件在某个时候可以通过stdin请求用户输入,但是它也可以打印出一个错误 为此,我使用的是subprocess.Popen,但是当前我在尝试读取输出和错误输出时遇到了问题,如果需要,请写入stdin并再次执行相同的操作 但是,当使用Popen.stderr.read()时,它会冻结,因为读取时没有错误;当使用Popen.communicate()获取输出时,我无法再向进程的STDIN发送更多数据 我怎样才能避开这个问题呢?这是我的密码:

我正在用Python编写一个与jar文件交互的脚本,jar文件在某个时候可以通过stdin请求用户输入,但是它也可以打印出一个错误

为此,我使用的是
subprocess.Popen
,但是当前我在尝试读取输出和错误输出时遇到了问题,如果需要,请写入stdin并再次执行相同的操作

但是,当使用
Popen.stderr.read()
时,它会冻结,因为读取时没有错误;当使用
Popen.communicate()
获取输出时,我无法再向进程的STDIN发送更多数据

我怎样才能避开这个问题呢?这是我的密码:

from subprocess import Popen, PIPE

with Popen (["java","-jar","something.jar"], stdin=PIPE, stdout=PIPE, stderr=PIPE) as process:
    # Closes STDIN, unable to communicate back
    # stdout, stderr = process.communicate()

    # Hangs forever if no STDERR is ever printed
    # stdout = process.stdout.read()
    # stderr = process.stderr.read()

相关的,甚至可能是重复的:相关的,甚至可能是重复的: