2017-08-20 24 views
0

私はpythonから結果の文字列(javaはSystem.out.printlnを実行する)をPythonリストに保存することにより、Java jarをPythonから呼び出す方法をスピードアップしようとしています。それは、javaからのファイルにdirectlに書き込むために使用されていましたが、私は非常に遅い、それを高速化しようとするたびにファイルを開いて閉じることがわかった。私はSTDOUTを使用することができますhereを読ん効率的な方法でPythonにjava jar出力を渡す

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

ので、私が試した::

は、私が現在持っている

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 

私は通りもはやファイルに書き込むことはできません。コールからポーペンに切り替えることができると信じています。

しかし、速度が非常に重要である(何百万回も実行されている)ので、何も端末に書き込まれないことが不可欠です。

System.out.printlnをjavaからpythonリストに渡す最も速い方法は何ですか?

答えて

0

TRY -

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

print output 
関連する問題