Calling Python from Java (Tomcat6) as sub-process

北慕城南 提交于 2019-12-11 19:33:03

问题


I am trying to call a python script from a java/tomcat6 webapp. I am currently using the following code:

           Process p = Runtime.getRuntime().exec("python <file.py>"); 
       InputStream in = p.getInputStream();
       InputStreamReader isr = new InputStreamReader(in); 
       BufferedReader b = new BufferedReader(isr);

       logger.info("PYTHON OUTPUT");


       String line = null;
       while ( (line = b.readLine()) != null){
            logger.info(line);
       }

       p.waitFor();
       logger.info("COMPLETE PYTHON OUTPUT");
       logger.info("EXIT VALUE: "+p.exitValue());

I can't really see any output in the catalinia.out file from the python script and using an adapter library like jython is not possible as the script relies on several machine learning libraries that need python's Numpy module to work.

Help?


回答1:


The explanation is probably one (or more) of following:

  • The command is failing and writing error messages to its "stderr" fd ... which you are not looking at.

  • The command is failing to launch because the command name is incorrect; e.g. it can't be found on $PATH.

  • The command is trying to read from its stdin fd ... but you haven't provided any input (yet).

  • It could be a problem with command-line splitting; e.g if you are using pathnames with embedded spaces, or other things that would normally be handled by the shell.

Also, since this is python, this could be a problem with python-specific environment variables, the current directory and/or the effective user that is executing the command.


How to proceed:

  1. Determine if the python command is actually starting. For instance. "hack" the "" to write something to a temporary file on startup.

  2. Change to using ProcessBuilder to create the Process object. This will give you more control over the streams and how they are handled.

  3. Find out what is going to the child processes "stderr". (ProcessBuilder allows you to redirect it to "stdout" ...)



来源:https://stackoverflow.com/questions/17756721/calling-python-from-java-tomcat6-as-sub-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!