我有兩個腳本,一個控制另一個並通過stdin與它通信。父腳本:子進程popen:爲什麼所有的寫入都會在子進程中立即發生?
import subprocess
import time
p = subprocess.Popen(['python','read_from_stdin.py'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
for i in range(0,10):
p.stdin.write(str(i))
p.stdin.write('\r\n') # \n is not sufficient on Windows
p.stdin.flush()
print i
time.sleep(1)
p.stdin.close()
孩子腳本(稱爲「read_from_stdin.py」):
import sys
import datetime
with open(datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + '.txt','w') as f:
for line in sys.stdin:
f.write(datetime.datetime.now().isoformat() + ' ' + line)
在那種年代由孩子腳本所有的輸入具有相同的時間戳創建的文件,儘管被寫入除了父腳本以外,儘管使用了flush()。
只想發表:) http://stackoverflow.com/questions/8416586/turn-off-buffering它*不*等待EOF,但有*緩衝*。請更新您的答案。 –
@ KarolyHorvath:它不等待EOF是正確的,但是你連接的問題是不相關的(修復'grep -line-buffered'在這裏不起作用 - OP已經調用了p.stdin。齊平()')。問題是Python 2中的預讀錯誤,在sys.stdin中有'for line。解決方法是:'對於行iter(sys.stdin.readline,'')' – jfs
@ J.F.Sebastian:*嘆*。檢查頂部投票答案... –