为避免以可移植的方式出现死锁,请在单独的线程中写入子代:
#!/usr/bin/env pythonfrom subprocess import Popen, PIPEfrom threading import Threaddef pump_input(pipe, lines): with pipe: for line in lines: pipe.write(line)p = Popen(path, stdin=PIPE, stdout=PIPE, bufsize=1)Thread(target=pump_input, args=[p.stdin, lines]).start()with p.stdout: for line in iter(p.stdout.readline, b''): # read output print line,p.wait()
请参阅Python:从subprocess.communicate()读取流输入



