title: Interprogram communication in python on Linux
body: <p>There's many examples on how to do these things to be found: </p>
  
  <p>1) Communicate between diffrent processes in the same program. </p>
  
  <p>2) Communicate between client/server over a network </p>
  
  <p>However, this question has no good example anywhere I've looked: </p>
  
  <ul>
  <li><strong>What is a canonical way to send a string from python program A to program B, which blocks and processes this string and then waits for another in a loop</strong>?</li>
  </ul>
  
  <p>I feel like I've come close to an answer many times, but never managed to create a working example. </p>
  
  <p>Additional implied requirements: </p>
  
  <ul>
  <li>Actually two diffrent programs: the example needs to actually have two diffrent programs (i.e. two files progA.py, progB.py that may be separately run from the commandline in two screens on the same machine), not use any kind of forking or multiprocess to create the client and server. </li>
  <li>Please suggest a way of doing this that allows variable length delimited strings up to a reasonable length to be sent, instead of having to get the exact byte count of the data size correct. (The latter is much more error-prone in implementation). </li>
  <li>Ideally do this without utilizing localhost internet connections</li>
  </ul>
  
  <p>For example; when the reader uses: </p>
  
  <pre><code>pipein = open(pipe_name, 'r')
  while program.KeepRunning:
      action = pipein.readline()[:-1]
      program.processLine(line)
      time.sleep(1)
  </code></pre>
  
  <p>and the writer uses: </p>
  
  <pre><code>command = "enable"
  pipeout = os.open(pipe_name, os.O_WRONLY)
  os.write(pipeout, command)
  os.write(pipeout, "\n")
  </code></pre>
  
  <p>as is suggested at <a href="http://www.python-course.eu/pipes.php">http://www.python-course.eu/pipes.php</a> 
  the reader gets stuck in an infinite loop reading out the empty string. </p>
  
  <p>Interestingly, adding <code>if(action == 'enable'): longFunction()</code> to the <code>program.processLine</code> function results in part of the code in the <code>longFunction</code> being executed before getting stuck reading out empty lines forever. </p>
  
  <p>On the other hand, all examples utilizing the more modern less low-level <code>subprocess</code> module only involve multi-threaded applications, not multiple applications. Other implementations involve sockets and networking. </p>
  
  <p>While I've tried utilizing sockets, this results in the generic 'something went wrong'-type error with many possible causes <code>Error 111: “connection refused”</code> showing up 'some of the time'. As part of the python code that is executed upon receiving certain commands actually modifies the network config (e.g. it calls commands such as <code>ip</code>, <code>tc</code> and <code>iptables</code> with various arguments) utilizing a network connection to <code>localhost</code> is something that probably should be avoided, causing hard to debug and generally nasty issues. Besides the part where it's unneccessary as the second program runs on the same machine, so any interprogram communication should not need to use network interfaces. </p>