PumpStreamHandler可以实时捕获过程输出 [英] PumpStreamHandler can capture the process output in realtime

查看:757
本文介绍了PumpStreamHandler可以实时捕获过程输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过apache-commons-exec捕获python进程输出.但是看起来它不会打印输出,只有在python进程完成后才显示输出.

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.

这是我的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进程退出后才获得输出,但是我想要的是实时获取输出)

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)

推荐答案

我找到了答案,我应该将set flush设置为true.例如

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

print('hello world', flush=True)

这篇关于PumpStreamHandler可以实时捕获过程输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆