JAVA web调用执行python脚本程序的四种方式,回避java.lang.OutOfMemoryError:PermGen space内存溢出问题

我在网上搜到的JAVA调用python程序的三种方式:

方式一:调用python函数。可以传入参数,获取返回值。

                      public static void PythonFunctionTest(){
                        //python 解释器
                        PythonInterpreter  interpreter = new PythonInterpreter();  
                        interpreter.execfile("E:\\Python_code\\helloPython.py");
                        PyFunction func=(PyFunction)interpreter.get("adder", PyFunction.class);
            
                        int a=2010;
                       int b=2;
                       PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));  
                       System.out.println("answer= "+pyobj.toString());
                     }
方式二:直接执行python脚本。不能传参,不能获取返回值。

                 public static void PythonTextTest()
                {
                   PythonInterpreter  interpreter = new PythonInterpreter();  
                   interpreter.execfile("E:/Python_code/helloPython1.py");
                }

方式三:在JAVA代码里面直接写python程序,直接执行。这种方法显然可以把参数传进去,但是能不能获得返回值需要进一步研究。

                 public static void PythonTest(){
                   PythonInterpreter interpreter =new PythonInterpreter();
                   interpreter.exec("days=('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')");
                   interpreter.exec("print days[0]");
                }
考虑了一下,因为第一种方式可以以调用函数的形式,同时即可以传参又可以获取返回值,所以我选择了第一种方式用于在JAVA代码块中执行python程序。我在单独的一个JAVA程序中执行相应代码时,没有问题,可以传参也可以获得返回的结果。但是当我把这段代码放到现有的JAVAWEB的程序中时,会出现

java.lang.OutOfMemoryError:PermGen space内存溢出的问题。


于是我又按照网上的方法增到tomcat和eclipse的内存,结果无效。就这样卡了很久后,我找到了第四种执行python程序的方法,这个方法不会产生内存溢出的问题。

方式四:使用控制台执行python脚本,可以传参和获取返回值。

               public static String runTimeMethod(String name,String path) {
               try {
                    String cmd="python E:\\Python_code\\settingTest.py "+name+" "+path;
                    Process pr = Runtime.getRuntime().exec(cmd);
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                    pr.getInputStream()));
                    String line="";
//输出打印在控制台上的结果            
//                  while ((line = in.readLine()) != null) {
//                  System.out.println(line);
//                 }
//得到打印在控制台上的结果
                    while ((line = in.readLine()) != null) {
                    result+=line;
                   }
                   in.close();
                   pr.waitFor();
//                 System.out.println("end");
                   } catch (Exception e) {
                   e.printStackTrace();
                 }
                return result;
    }
其实这种方法就是在JAVA程序中执行控制台命令,其效果等同于把
python E:\\Python_code\\settingTest.py  arg1 arg2
这段命令拷贝到cmd控制台中。

虽然可以传入参数或获取返回值,但其实它的实现并不是特别灵活和好用。下面是我用于测试的python代码。

#coding=utf-8
import json
import sys
def JsonTestForDownloadApks(name,path):
    print name,'   ',path 
name=sys.argv[1]
path=sys.argv[2]
JsonTestForDownloadApks(name,path)
可以看出,传入参数的实现是通过python程序获取控制台输入的方式。sys.arg[0]是当前python程序的执行路径,sys.arg[1],sys.arg[2]是我们输入的第一,二个参数。获取返回结果是通过获取打印到控制台的输出流获的方式。其实这种方式有很大的局限性,但是它起码能回避让我头疼的内存溢出问题,也能满足我目前的功能需求,所以我选择了这种方式。

PS.因为我对python的知识几乎一无所知,对于python和JAVA之间的调用也是现学现用,所以我上面所说的方法可能非常低级甚至存在很大的风险,仅供大家参考。



你可能感兴趣的:(JAVA web调用执行python脚本程序的四种方式,回避java.lang.OutOfMemoryError:PermGen space内存溢出问题)