以有效的方式将java jar输出传递给python



我试图通过将生成的字符串(java do System.out.println(保存在python列表中来加快从python调用java jar的方式。它曾经从java写入定向到文件,但我发现这非常慢,每次打开和关闭文件,所以试图加快速度。

我目前有:

subprocess.call(
['java', '-jar', 'TensionTwoSlices.jar', '-runtype', '"groundtruth"', '-inputwindow', inputwindow, '-outputwindow', groundtruth,
'-inputfile', '"not.txt"', '-key', '"C"'],stdout=subprocess.PIPE, stderr=subprocess.PIPE);

我在这里读到我可以使用STDOUT,所以我尝试了:

p = subprocess.Popen(
['java', '-jar', 'TensionTwoSlices.jar', '-runtype', '"groundtruth"', '-inputwindow', inputwindow, '-outputwindow', groundtruth,
'-inputfile', '"not.txt"', '-key', '"C"'],stdout=subprocess.STDOUT, stderr=subprocess.PIPE);

print p.STDOUT;

但我得到:

Traceback (most recent call last):
File "extract_tension.py", line 65, in <module>
'-inputfile', '"not.txt"', '-key', '"C"'],stdout=subprocess.STDOUT, stderr=subprocess.PIPE);
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 9] Bad file descriptor

由于我不再写入文件,我相信我可以从调用切换到 Popen。

但至关重要的是,没有任何东西写入终端,因为速度非常重要(运行数百万次(。

将 System.out.println 从 java 传递到 python 列表的最快方法是什么?

试试 -

output = subprocess.check_output(
['java', '-jar', 'TensionTwoSlices.jar', '-runtype', 
'"groundtruth"', '-inputwindow', inputwindow, '-outputwindow', 
groundtruth, '-inputfile', '"not.txt"', '-key', '"C"'])
print output

最新更新