我尝试通过apache-commons-exec捕获python进程输出。但是它看起来不会打印输出,输出仅在python进程结束后显示。
下面是我的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代码(我只在python进程退出后获得输出,但我想要的是实时获得输出)
for i in range(1,10):
print(i)
import time
time.sleep(10)发布于 2017-08-01 16:59:36
如果我找到了答案,我应该将flush设置为true。例如。
print('hello world', flush=True)https://stackoverflow.com/questions/45433206
复制相似问题