我有一個名爲1st.py
腳本創建一個REPL(讀-EVAL-打印循環):瞭解Popen.communicate
print "Something to print"
while True:
r = raw_input()
if r == 'n':
print "exiting"
break
else:
print "continuing"
然後我用下面的代碼推出1st.py
:
p = subprocess.Popen(["python","1st.py"], stdin=PIPE, stdout=PIPE)
然後試過這個:
print p.communicate()[0]
它失敗了,提供了這個回溯:
Traceback (most recent call last):
File "1st.py", line 3, in <module>
r = raw_input()
EOFError: EOF when reading a line
你能解釋一下這裏發生了什麼嗎?當我使用p.stdout.read()
時,它永遠掛起。
可以告訴我 print >> p.stdin,i和p.stdin.write(i) –
這裏'print'是'p.stdin.write(str(i)+「\ n」 ); p.stdin.flush()'。 – jfs
謝謝... 還有一件事請告訴我這個bufsize = 1在做什麼?還有Popen中的「-u」([「python」,「-u」,「1st.py」],stdin = PIPE,stdout = PIPE,bufsize = 1) –