title: Background process in Python
body: <p>I need to create a background process that will wait for incoming commands and perfom them. Here's the code:</p>
  
  <pre><code>instance_tuple.popen = subprocess.Popen(['python',\
                                      os.path.join(config['scripts_dir'],\
                                      'instance_script.py')],\
                                      stdin = subprocess.PIPE,\
                                      stdout = subprocess.PIPE)
  </code></pre>
  
  <p>Process function code:</p>
  
  <pre><code>if __name__ == '__main__':
      config = dict()
      is_config_valid = False
      print 'Hello from instance process'
      while True:
          cmd_str = raw_input()
          if (cmd_str.strip() != ''):
              print 'received %s' % cmd_str
              command = json.loads(cmd_str)
          print 'received command: %s' % str(command)
          sys.stdout.flush()
          if command['name'] == 'set_variable':
              name = command['args'][0]
              value = command['args'][1]
              config[name] = value
              is_config_valid = validate_instance_dict(config)            
          elif is_config_valid:
              if (command['name'] == 'init_model'):
                  config['instance'].init_model()
              elif (command['name'] == 'get_tree'):
                  tree = config['instance'].get_fidesys_tree(command['args'])
                  result = CommandResult(command.name, tree)
      print 'process exit'
  </code></pre>
  
  <p>That's how I send data to the process:
  1st test run works ok:</p>
  
  <pre><code>(input, errors) = instance_tuple.popen \
                    .communicate(json.dumps({'name': 'name', 'args': list()}))
  </code></pre>
  
  <p>Later for some reason <code>raw_input()</code> gets a EOF and the process exits. What is the correct way to setup an interprocess communication?</p>