Python子进程通信杀死了我的进程

use*_*920 3 python subprocess

为什么通信会杀死我的进程?我想要一个交互式过程,但通信做了一些事情,这样我就不能再在我的过程中接受 raw_input 了。

from sys import stdin 
from threading import Thread
from time import sleep

if __name__ == '__main__':
    print("Still Running\n")
    x = raw_input()    
    i = 0
    while ('n' not in x ) :
        print("Still Running " + str(i) + " \r\n")
        x = raw_input()
        i += 1

    print("quit")



print(aSubProc.theProcess.communicate('y'))
print(aSubProc.theProcess.communicate('y'))
Run Code Online (Sandbox Code Playgroud)

例外!

self.stdin.write(input)
ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)

Ray*_*nda 5

communicate和对象wait的方法,在进程返回后Popen关闭。PIPE如果您想与流程保持沟通,请尝试以下操作:

import subprocess
proc = subprocess.Popen("some_process", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
proc.stdin.write("input")
proc.stdout.readline()
Run Code Online (Sandbox Code Playgroud)

  • 您的代码由于缓冲而导致死锁。请参阅[为什么不只使用管道(popen())?](http://pexpect.readthedocs.org/en/latest/FAQ.html#whynotpipe) 另外,`proc.communicate()` 在看到管道时会关闭管道EOF 可能发生在子进程退出之前/之后。`proc.wait()` 不会关闭任何管道。它等待子进程退出。 (5认同)