无法在进程之间建立管道

2024-09-28 17:20:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在父进程和子进程之间创建管道:

def launch_process():

    parentStdin, childStdout = os.pipe()
    childStdin, parentStdout = os.pipe()

    pid = os.fork()
    if pid:
        os.close(childStdout)
        os.close(childStdin)
        self.set_configuration()            
    else:
        os.close(parentStdout)
        os.close(parentStdin)
        os.dup2(childStdin, 0)
        os.dup2(childStdout, 1)            
        os.execvp("echo.py", "")
        raise RuntimeError

#after launch_process() call...
pipein = os.fdopen(parentStdin) #I saved both parentStdin and parentStdout variable as global
while True:
    time.sleep(3)
    os.write(parentStdout, ("command").encode())
    line = pipein.readline() #the program stops here
    print(line)

此代码可以启动子进程,但pepin.readline()块运行时没有任何结果。怎么了?你知道吗

编辑:子进程运行以下循环回显程序(“回音.py“”:

while True:
    value = input()
    print(value)
    sys.stdout.flush()

Tags: pyclose进程oslaunchprocesspidpipe