PumpStreamHandler can capture the process output in realtime

微笑、不失礼 提交于 2019-12-12 04:04:56

问题


I try to capture a python process output via apache-commons-exec. But it looks like it won't print the output, the output is only displayed after I the python process is finished.

Here's my java code

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);

And this is the python code I want to execute (I only get the output after the python process is exited, but what I want is to get the output in real time)

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

import time

time.sleep(10)

回答1:


I find the answner, I should use set flush to true. E.g.

print('hello world', flush=True)


来源:https://stackoverflow.com/questions/45433206/pumpstreamhandler-can-capture-the-process-output-in-realtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!