python under java2

Steve Horsley shoot at the.moon
Mon Feb 7 16:24:12 EST 2005


motokoit wrote:
> For some reason i need to start a python script from inside a java code. The
> main part of the code is something like this
> 
> try{
>    Runtime rt = Runtime.getRuntime();
>    System.out.println("start");
>    Process proc =  Runtime.getRuntime().exec("python myscript.py");
>    proc.waitFor();
>    System.out.println("end");
> } catch (Exception e){
>    e.printStackTrace();
> }
> 
> and so worked for months....but since some days ago the same instruction
> does not work but execute without any exception, that is print either
> "start" and "end"; the strangest thing happen when I call just
> 
> Process proc =  Runtime.getRuntime().exec("python myscript.py");
> 
> the execution looks like stalled and does not comes to an end printing just
> the "start".
> 
> Some suggestion?
> My machine is a Linux Mandrake64 10.1
> 
I think this is probably really a java question. I suspect that your
python script is issuing too much output either on stdout or stderr,
and because you are not reading these streams, the process is blocking
because the output buffers are full. If this is the case, you need to 
get your java program to read the streams (Process.getInputStream()
and Process.getErrorStream()), which is something you should be doing
anyway. In order to read both streams, you either need two threads, or
to use java.nio and select.

A short term fix might be to run theh python script by hand and see if
it throws out a long stack trace (this could block an unread error stream).

HTH
Steve



More information about the Python-list mailing list