Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 子进程中的sys.stdin.read()永不返回_Python_Python 3.x_Io - Fatal编程技术网

Python 子进程中的sys.stdin.read()永不返回

Python 子进程中的sys.stdin.read()永不返回,python,python-3.x,io,Python,Python 3.x,Io,我试图理解subprocess.Popen和subprocess.PIPE。在这样做的同时,我创建了三个小脚本 本练习的目标是,使用一个脚本打印数据,从另一个脚本读取数据,并将其回显到第一个脚本 问题是,最后一个sys.stdin.read()会阻止整个代码正常工作 test.py #!/usr/bin/python3 from subprocess import Popen, PIPE proc1 = Popen(["python3","script1.py"], stdin=PIPE,

我试图理解subprocess.Popen和subprocess.PIPE。在这样做的同时,我创建了三个小脚本

本练习的目标是,使用一个脚本打印数据,从另一个脚本读取数据,并将其回显到第一个脚本

问题是,最后一个sys.stdin.read()会阻止整个代码正常工作

test.py

#!/usr/bin/python3

from subprocess import Popen, PIPE

proc1 = Popen(["python3","script1.py"], stdin=PIPE, stdout=PIPE)
proc2 = Popen(["python3","script2.py"], stdin=PIPE, stdout=PIPE)

first_output = proc1.stdout.read()

print("[test.py:] " + str(first_output))

proc2.stdin.write(first_output)
proc2.stdin.flush()
proc2.stdin.close()

answer = proc2.stdout.read()

print("[test.py:] " + str(answer))

proc1.stdin.write(answer)
import sys

sys.stdout.write("Hello world")
sys.stdout.flush()

answer = str(sys.stdin.read())
import sys

input_read = str(sys.stdin.read())

sys.stdout.write("[script2.py:] " + input_read + " input read")
sys.stdout.flush()
script1.py

#!/usr/bin/python3

from subprocess import Popen, PIPE

proc1 = Popen(["python3","script1.py"], stdin=PIPE, stdout=PIPE)
proc2 = Popen(["python3","script2.py"], stdin=PIPE, stdout=PIPE)

first_output = proc1.stdout.read()

print("[test.py:] " + str(first_output))

proc2.stdin.write(first_output)
proc2.stdin.flush()
proc2.stdin.close()

answer = proc2.stdout.read()

print("[test.py:] " + str(answer))

proc1.stdin.write(answer)
import sys

sys.stdout.write("Hello world")
sys.stdout.flush()

answer = str(sys.stdin.read())
import sys

input_read = str(sys.stdin.read())

sys.stdout.write("[script2.py:] " + input_read + " input read")
sys.stdout.flush()
script1.py
answer=str(sys.stdin.read())
中的最后一行导致整个程序卡住。如果我把它评论出来,一切都很好

为什么会这样?为什么我无法进一步沟通?我还没有找到答案

script2.py

#!/usr/bin/python3

from subprocess import Popen, PIPE

proc1 = Popen(["python3","script1.py"], stdin=PIPE, stdout=PIPE)
proc2 = Popen(["python3","script2.py"], stdin=PIPE, stdout=PIPE)

first_output = proc1.stdout.read()

print("[test.py:] " + str(first_output))

proc2.stdin.write(first_output)
proc2.stdin.flush()
proc2.stdin.close()

answer = proc2.stdout.read()

print("[test.py:] " + str(answer))

proc1.stdin.write(answer)
import sys

sys.stdout.write("Hello world")
sys.stdout.flush()

answer = str(sys.stdin.read())
import sys

input_read = str(sys.stdin.read())

sys.stdout.write("[script2.py:] " + input_read + " input read")
sys.stdout.flush()
当您这样做时:

first_output = proc1.stdout.read()
这会尝试读取
proc1
所说的所有内容;也就是说,它一直读取,直到文件句柄关闭。这不会发生,因为
proc1
本身正在等待读取:

answer = str(sys.stdin.read())
使用管道在进程之间进行通信可能有点棘手。我建议通读一遍

为了解决你眼前的问题,我知道两个简单的解决办法。一种是切换到在线交流。在
script1.py
中,写一行新行:

sys.stdout.write("Hello world\n")
sys.stdouf.flush()

answer = str(sys.stdin.readline())
在script2.py中添加一个换行符并切换到
readline

input_read = str(sys.stdin.readline())

sys.stdout.write("[script2.py:] " + input_read + " input read\n")
sys.stdout.flush()

另一种方法是切换到使用而不是读取。要读取的字节数需要一个参数,但在返回数据之前不要等到接收到那么多数据。

谢谢您指出这一点。是否最好在“按预期”这行附近写点东西,或者您建议我将标题改为什么?嗯,subprocess never returning中的sys.stdin.read()将描述该行为,但不会暗示这是因为系统设施未按照规范运行。感谢您指出我的错误所在。我发现在两个进程之间实现通信有点困难。我将尝试使用readline()。我已经阅读了关于子流程的文档。我将更仔细地查看阅读的文档。它按照我的预期工作。readline()是一个不错的选择。非常感谢。很高兴能帮上忙!