TheJavaException
TheJavaException

Reputation: 11

Python process never finishing when called from Java

I've tried to setup an AI with PyTorch. Everything is fine when I call my script from the console. But when I call the script in a Java `ProcessBuildera, it will finish but never terminate...

Here is the ProcessBuilder code

String[] cmd = {"python3", "-i" , "AI/Home-System.py",
                data.getName().replace(".csv", ""),
                "true",
                "false"};
ProcessBuilder pb = new ProcessBuilder(cmd);
Process p = pb.start();

Hope that you can help me

Edit:

I found another solution. I call this script in a linux screen with

String[] cmd = {"screen", "-dmS", "AI-" + device,
                "python3", "AI/Home-System.py",
                data.getName().replace(".csv", ""),
                "true",
                "false"};
Runtime.getRuntime().exec(cmd);

Upvotes: 1

Views: 165

Answers (1)

Joshua Fox
Joshua Fox

Reputation: 19655

Read the process' output stream, as the end of this stream allows your ProcessBuilder to exit. Or else call the ProcessBuilder's inheritIO().

Then waitFor() the process.

Here is some sample code showing these steps.

Upvotes: 2

Related Questions