简体   繁体   English

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

[英]sys.stdin.read() in subprocess never returning

I'm trying to understand subprocess.Popen and subprocess.PIPE. 我正在尝试理解subprocess.Popen和subprocess.PIPE。 While doing so, I created three small scripts. 在这样做的同时,我创建了三个小脚本。

The goal of this exercise is, to print data with one script, read it from the other and echo this back to the first script. 本练习的目标是,使用一个脚本打印数据,从另一个脚本中读取数据并将其回显给第一个脚本。

The problem is, that the last sys.stdin.read() prevents the entire code from working properly. 问题是,最后一个sys.stdin.read()阻止整个代码正常工作。

test.py 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)

script1.py script1.py

import sys

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

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

The last line in script1.py answer = str(sys.stdin.read()) causes the entire program to get stuck. script1.py answer = str(sys.stdin.read())的最后一行导致整个程序卡住。 If I comment it out, everything works fine. 如果我发表评论,一切正常。

Why is that so and why am I not able to communicate any further? 为什么会这样,为什么我无法进一步沟通? I didn't find an answer yet. 我还没有找到答案。

script2.py script2.py

import sys

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

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

When you do: 当你这样做时:

first_output = proc1.stdout.read()

this tries to read everything proc1 has to say; 这试图读取proc1必须说的一切; that is, it reads until the file handle is closed. 也就是说,它会在文件句柄关闭之前读取。 That doesn't happen, because proc1 is itself waiting to read: 这不会发生,因为proc1本身正在等待阅读:

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

Communicating between processes using pipes can be a bit tricky. 使用管道在进程之间进行通信可能有点棘手。 I'd recommend reading through the documentation for the subprocess module . 我建议阅读子进程模块的文档

To solve your immediate problem, I know of two simple solutions. 为了解决您的问题,我知道两个简单的解决方案。 One would be to switch to communicating in lines. 一种是切换到线路通信。 In script1.py , write a newline: script1.py ,写一个换行符:

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

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

And in script2.py add a newline and switch to readline as well: 在script2.py中添加换行符并切换到readline

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

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

The other approach would be to switch to using read1 instead of read. 另一种方法是切换到使用read1而不是read。 It takes an argument for the number of bytes to read, but doesn't wait to receive that much data before returning it. 它需要一个参数来读取字节数,但是在返回之前不会等待接收那么多数据。

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

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