title: Python - communicate with subprocess
body: <p>I'm running Python 2.7 on a Win32 OS, but i'm hoping to write platform-independent code. I'm trying to use Python to interact in real-time with a Java program i wrote, and figured pipes would be the best way to do this. My Python script is calling Java as a subprocess. Essentially, Java is the GUI and Python is the back end. (I don't want to use Jython or wxPython because i only want to be dependent upon the standard libraries each language provides.) Trying to set up communication between the two has been terrible. I can send a message from the (parent) Python script to the (child) Java class using</p>
  
  <pre><code>process.stdin.write(msg)
  process.stdin.flush()
  </code></pre>
  
  <p>but reading Java's output has not worked. I use</p>
  
  <pre><code>process.stdout.read()
  </code></pre>
  
  <p>but apparently this blocks forever if there's nothing to read. And <code>process.communicate()</code> is off limits because it doesn't do anything until the subprocess terminates. According to my research, a common method people use to get around this problem is to "use threads" (although someone suggested appending a newline when writing -- didn't work), but being new to Python and threading in general i have no idea how that would look. I've tried looking over the standard library's <code>subprocess.py</code> source but that hasn't helped. Is there a way to see if stdout is empty, at least? If not, how do i accomplish this?</p>