0

As I know pretty much nothing about Python, I'm trying to run a python script that I need from Java. I don't need to interface with it's interfaces or anything else, just simply start it with certain arguments.

Following other overflow threads like this one, I am starting the script as follows:

String longitude = "snip"; // Will be a number like +/-xx.xxxxxxx
String latitude = "snip"; // Will be a number like +/-xx.xxxxxxx
String[] command = {"python", "example.py",
        "-a", "ptc",
        "-u", "snip", // Username
        "-p", "snip", // Password
        "-l", longitude + " " + latitude,
            "-st", "10"
};
ProcessBuilder probuilder = new ProcessBuilder(command);
probuilder.directory(new File("snip")); // Simply a folder where file is
Process p = probuilder.start();

After this I just have a few lines that capture the output of the process and output it to System.out, then run Process.waitFor() so the main thread doesn't return until the python finishes. The issue is, the python script I'm running (found here) uses multiple threads, and I believe it's unable to start all threads. I get output from Flask, but then nothing happens yet Process.waitFor() does not return. Is there a way to correctly run multithreaded python scripts through java?

For reference, here is the output I'm getting, and here is expected output.

2
  • What are you doing with the ProcessBuilder stdout? Since by default it just goes into pipes, you may be blocking once the pipe is filled up. Take a look at ProcessBuilder.inheritIO( ) or ProcessBuilder.redirectOutput( ) - docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html Commented Jul 20, 2016 at 22:05
  • @user3745362 I'm using the exact solution from the thread at the end of this message. Could your mentioned problem still exist here? stackoverflow.com/questions/1732455/… EDIT: Changing to your method didn't fix the issue Commented Jul 20, 2016 at 22:15

0

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.