Commit 7c2dd41c by guominglei

测试python调用

parent b2a27959
...@@ -5,29 +5,46 @@ import org.apache.commons.exec.DefaultExecutor; ...@@ -5,29 +5,46 @@ import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog; import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler; import org.apache.commons.exec.PumpStreamHandler;
import java.io.*; import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class TestPy { public class TestPy {
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream,outputStream,null); List<ExecuteWatchdog> watchdogList = new ArrayList<>();
ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
DefaultExecutor defaultExecutor = new DefaultExecutor(); Thread thread = new Thread(() -> {
defaultExecutor.setWatchdog(watchdog); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
defaultExecutor.setExitValues(null); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream,outputStream,null);
defaultExecutor.setStreamHandler(pumpStreamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
CommandLine commandline = new CommandLine("python"); watchdogList.add(watchdog);
commandline.addArgument("D:\\2020project\\byit-myth-job\\demo-client\\byit-demo-client\\test.py"); DefaultExecutor defaultExecutor = new DefaultExecutor();
int execute = defaultExecutor.execute(commandline); defaultExecutor.setWatchdog(watchdog);
byte[] bytes = outputStream.toByteArray(); defaultExecutor.setExitValues(null);
Thread.sleep(3000); defaultExecutor.setStreamHandler(pumpStreamHandler);
watchdog.destroyProcess(); CommandLine commandline = new CommandLine("python");
System.out.println(execute); commandline.addArgument("D:\\workspace\\pycharmWorkSpace\\test\\com.test\\Test.py");
int execute = 0;
try {
System.out.println(new String(bytes,0,bytes.length, StandardCharsets.UTF_8)); execute = defaultExecutor.execute(commandline);
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = outputStream.toByteArray();
System.out.println(execute);
System.out.println(new String(bytes,0,bytes.length, StandardCharsets.UTF_8));
});
thread.start();
Thread.sleep(10000);
watchdogList.forEach(watchdog ->{
watchdog.destroyProcess();
});
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment