從python代碼中,我想運行一個二進制程序,從stdin中獲取它的參數。使用子進程模塊,這應該是直接的:subprocess.Popen和緩衝過程輸出
import subprocess
command = [ 'my_program' ]
p = subprocess.Popen(command, \
stdin = subprocess.PIPE, stdout = subprocess.PIPE, \
env={ "GFORTRAN_UNBUFFERED_ALL": "1"})
p.stdin.write (stdin_stuff)
while True:
o = p.stdout.readline()
if p.poll() != None:
break
# Do something with stdout
現在,這啓動程序,但python腳本只是掛在那裏。我明白,這可能是由於gfortran(我用它來編譯my_program緩衝它的標準輸出流,gfortran允許使用GFORTRAN_UNBUFFERED_ALL環境變量,就像我所做的那樣),以及在Fortran代碼中使用FLUSH()內部函數,但仍沒有運氣:在Python代碼仍然掛起
您可以通過「檢查」旁邊的標記來接受您喜歡的答案。在這個網站上這被認爲是禮貌的,所以你可能想要檢查你所問的問題最滿意的答案。 – 2011-02-09 23:31:38