title: How to return a dictionary as a function's return value running as a subprocess to its parent process?
body: <p>I have two scripts parent.py and child.py The parent.py calls child.py as a subprocess. Child.py has a function that collects certain result in a dictionary and i wish to return that dictionary back to the parent process. I have tried by printing that dictionary from child.py onto its STDOUT so that the parent process can read it but then thats not helping me as the dictionary's content are being read as strings on seperate lines by the parent.</p>
  <p>Moreover , as suggested in comments , i tried serializing the dictionary using JSON while printing it on stdout and also read it back from the parent using JSON , that works fine, but i also am printing a lot of other information from the child to its stdout which is eventually also being read by the parent and is mixing things up .</p>
  <p>Another suggestion that came up was by writing the result from the child to a file in the directory and make the parent read from that file. That would work too , but i would be running 100s of instances of this code in Celery and hence it would lead to overwrites on that same file by other instances of the child.</p>
  <p>My question is since we have a PIPE connecting the two processes how can i just write my dictionary directly into the PIPE from child.py and get it read from the parent.py</p>
  <pre><code># parent.py
  
  import subprocess
  
  proc = subprocess.Popen(['python3', 'child.py'],
                          stdin=subprocess.PIPE,
                          stdout = subprocess.PIPE
                          )
  proc.comunicate()
  result = proc.stdout
  </code></pre>
  <pre><code>#child.py
  
  def child_function():
      result = {}
      result[1] = &quot;one&quot;
      result[2] = &quot;two&quot;
      print(result)
      #return result
      
  if __name__ == &quot;__main__&quot;:
      child_function()
  </code></pre>