0

I am trying to speed up the way I call a java jar from python by saving the resulting string (java does System.out.println) in a python list. It used to write to directl to a file from java, but I found that was very slow, opening and closing the file each time, so trying to speed it up.

I currently have:

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

I read here that I can use STDOUT, so I tried:

 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;

but I am getting:

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

As I am not longer writing to a file I believe I can switch from call to Popen.

But it's essential that nothing is written to the terminal as speed is extremely important (running this millions of times).

What is the fastest way of passing the System.out.println from java to a python list?

1 Answer 1

0

Try -

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

print output
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.