E
E
EgorSvinarev2022-01-08 15:00:47
Python
EgorSvinarev, 2022-01-08 15:00:47

Why doesn't BufferedReader see EOF?

Hello. I need to read data from stdout for a process running the following

python code

from jupyter_client import MultiKernelManager

mngr = MultiKernelManager()
remote_id = mngr.start_kernel("python3")

print(remote_id)


In Java I handle this process like this:

Starting process method

private Process startProcess(String command) throws Exception {
  Process process = Runtime.getRuntime().exec(command);
    
  return process;
}


Method that reads data from streams

/** Reads the output stream from the process */
private String readStream(InputStream stream) throws Exception {
  InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
    
  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    
  StringBuilder stringBuilder = new StringBuilder();
  StringBuilder response = new StringBuilder();
    
  String[] splitArray;
  String msg;
    
  while ((msg = bufferedReader.readLine()) != null) {
    logger.debug(msg);
    stringBuilder.append(msg).append("\n");
  }
    
  logger.debug("Stream was read");
  
  String control = stringBuilder.toString();
  splitArray = control.split("\n");
    
  for (String s : splitArray) {
    response.append(s).append("\n");
  }
    
  bufferedReader.close();
  inputStreamReader.close();
    
  String output = response.toString();
  output = (output == null || output.length() == 0) ? "" : (output.substring(0, output.length() - 1));
        
  return output;

}



Thread objects are retrieved from the process and passed to the read method
spoiler

Process process = startProcess(command);	
String output = readStream(process.getInputStream());



However, I found that in this
place
while ((msg = bufferedReader.readLine()) != null) {
  logger.debug(msg);
  stringBuilder.append(msg).append("\n");
}
execution is interrupted

In this case, in theory, the execution of the process itself ends, and all data from the python program is returned.
What could be the problem? How can I read the data to the end?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Zhukov, 2022-01-31
@vip_delete

The Python process exits before it writes everything to stdout because it uses buffered output by default. To disable this, start the process with the -u flag.

Runtime.getRuntime().exec(new String[] { "python", "-u", "script.py" })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question