Process process = Runtime.getRuntime().exec("python Design-of-experiment-Python\\Main.py " + intValue + " " + sampleNums + " " + stringValue);
//缓冲区阻塞 造成死锁
// 等待 Python 脚本执行完毕
int exitCode = process.waitFor();
问题:使用Runtime.getRuntime().exec()传值调python 数据一直无返回 子进程阻塞
原因:python代码输出结果到缓冲区 由于 主进程这边 没有读出缓冲区数据 造成缓冲区满 子进程无法写入 导致阻塞;而主进程这边 等待子进程结束 也在等待 造成死锁
解决:加入读缓冲区即可
Process process = Runtime.getRuntime().exec("python Design-of-experiment-Python\\Main.py " + intValue + " " + sampleNums + " " + stringValue);
//缓冲区阻塞 造成死锁
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
// 等待 Python 脚本执行完毕
int exitCode = process.waitFor();