为什么通信会杀死我的进程?我想要一个交互式过程,但通信做了一些事情,这样我就不能再在我的过程中接受 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)
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)