I want to write a script that would work a bit like hadoop streaming: I provide a random "client" program's path, and from my host python script I "pipe" strings to the client, and I want to receive the client's stdout in my python program.
So for instance if I have the following python basic python client "client.py":
import sys
for line in sys.stdin:
print("printing : " + line)
I want, from my python host, to be able to call the executable "python client.py", provide it with the list ["a", "b"], and then receive ["printing a", "printing b"] as a result.
Here's what I tried in my host code:
import subprocess
proc = subprocess.Popen("python client.py",stdout=subprocess.PIPE, stdin=subprocess.PIPE)
for text in ["a", "b"]
print(text)
proc.stdin.write(bytes(text, 'UTF-8'))
result = proc.stdout.read()
print ("result " + str(result))
self.proc.wait()
However (on windows) it executes print(text), then open a python.exe windows that remains frozen....
Does anyone know how to accomplish what I'm trying to do ? should work on windows and linux ideally
edit: in my real application the amount of data to transfer to stdin is 10000s of lines of ~1K chars each, so I can't just send it all at once the content from stdout should be around 10000s of lines of 10 chars each
selectorsmodule, but that requires 3.4 or higher. Pre-3.4, you'd either use the lower level primitives from theselectmodule, or "cheat" and use two threads, one to feed the subprocess, one two consume its output.closeon thestdinof the child in the parent. Otherwise the final writes are not flushed, and even if they are flushed, the child has no way to know that there is no morestdinto read; it will block forever waiting on it.