1

I have a Java application that launches a Python process. The Python process opens up a socket and listens for incoming data that is sent to it. Python will make prints to the screen throughout its lifetime that my Java app needs to be able to read. My Java code is as follows:

Runtime runTime = Runtime.getRuntime();
Process proc = runTime.exec(command, null, dir);
reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = reader.readLine()) != null) 
{
    System.out.println(line);
}

This works for me in general, but as soon as I have my Python socket listen, I no longer receive anymore of the input stream. As shown below, when I comment out my receive within the Python script, my Java application will receive both prints.

print "socket waiting to receive"
#data, addr = sock.recvfrom(self.BUFFER_SIZE) # this line causing problems
print "socket received data"

If I uncomment the receive, I no longer get ANY prints within Java. I don't even get the print that occurs before the blocking recvfrom statement. If I run the script from the command line directly, I still get the "socket waiting to receive" and will get the "socket received data" print whenever I receive data.

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
Matt
  • 295
  • 3
  • 6
  • 17
  • Are you sure you cannot use Java to open the socket and not use python? – Peter Lawrey Sep 26 '13 at 19:23
  • 2
    [Disable stdout buffering in python](http://stackoverflow.com/questions/107705/python-output-buffering). – Piotr Praszmo Sep 26 '13 at 19:24
  • Unfortunately, I can't. The Python code is fairly complex overall and the Java app is mostly being used as a front-end GUI to determine what command line flags to pass into the program. I have an option of writing the GUI in Python, but I'd like to avoid that if possible. – Matt Sep 26 '13 at 19:28
  • @Banthar Ahh! That seems to have done the trick. I'll test some more after lunch, but my simple example I wrote above now works. Thank you so much! – Matt Sep 26 '13 at 19:30

0 Answers0