2017-08-01 356 views
0

我嘗試通過apache-commons-exec捕獲python進程輸出。但它看起來不會打印輸出,只有在python過程完成後才顯示輸出。PumpStreamHandler可以實時捕獲進程輸出

這裏是我的Java代碼

CommandLine cmd = CommandLine.parse("/Users/jzhang/anaconda/bin/python"); 
cmd.addArgument("/Users/jzhang/a.py"); 
DefaultExecutor executor = new DefaultExecutor(); 
ExecuteWatchdog watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); 
executor.setWatchdog(watchDog); 

executor.execute(cmd); 

這是Python代碼,我想蟒蛇處理退出後執行(我只得到輸出,但我要的是獲得實時輸出)

for i in range(1,10): 
    print(i) 

import time 

time.sleep(10) 

回答

0

我找到answner,我應該使用set flush爲true。例如。

print('hello world', flush=True)