下面的代码为什么不打印stdout和退出,而是挂在窗口上,我对此感到很困惑。有什么原因吗?
import subprocess
from subprocess import Popen
def main():
proc = Popen(
'C:/Python33/python.exe',
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
proc.stdin.write(b'exit()\r\n')
proc.stdin.flush()
print(proc.stdout.read(1))
if __name__=='__main__':
main()发布于 2013-12-21 07:13:38
替换如下:
proc.stdin.flush()通过以下方式:
proc.stdin.close()否则,子进程python.exe将永远等待stdin关闭。
备选方案:使用communicate()
proc = Popen(...)
out, err = proc.communicate(b'exit()\r\n')
print(out) # OR print(out[:1]) if you want only the first byte to be print.https://stackoverflow.com/questions/20716484
复制相似问题