How To Thread Multiple Subprocess Instances In Python 2.7?
I have three commands that would otherwise be easily chained together on the command-line like so: $ echo foo | firstCommand - | secondCommand - | thirdCommand - > finalOutput
Solution 1:
To emulate:
echo foo |
firstCommand - | somePythonRoutine - |
secondCommand - | anotherPythonRoutine - |
thirdCommand - > finalOutput
your current approach with threads works:
from subprocess import Popen, PIPE
first = Popen(["firstCommand", "-"], stdin=PIPE, stdout=PIPE, bufsize=1)
second = Popen(["secondCommand", "-"], stdin=PIPE, stdout=PIPE, bufsize=1)
bind(first.stdout, second.stdin, somePythonRoutine)
with open("finalOutput", "wb") as file:
third = Popen(["thirdCommand", "-"], stdin=PIPE, stdout=file, bufsize=1)
bind(second.stdout, third.stdin, anotherPythonRoutine)
# provide inputfor the pipeline
first.stdin.write(b"foo")
first.stdin.close()
# wait for it to complete
pipestatus = [p.wait() for p in [first, second, third]]
where each bind()
starts a new thread:
from threading import Thread
defbind(input_pipe, output_pipe, line_filter):
deff():
try:
for line initer(input_pipe.readline, b''):
line = line_filter(line)
if line:
output_pipe.write(line) # no flush unless newline presentfinally:
try:
output_pipe.close()
finally:
input_pipe.close()
t = Thread(target=f)
t.daemon = True# die if the program exits
t.start()
and somePythonRoutine
, anotherPythonRoutine
accept a single line and return it (possibly modified).
Solution 2:
The point of communicate()
is that it returns the output of the process. This collides with your pipe setup.
You should only call it once on the third process; all the other ones are connected via pipes and know how to communicate with each other - no other / manual intervention is necessary.
Post a Comment for "How To Thread Multiple Subprocess Instances In Python 2.7?"