If prog2
accepts input only as a filename i.e., if you need to emulate the bash process substitution: prog2 <(prog1 input_1.txt)
then to avoid writing the output of prog1
subprocess to disk, you could use /dev/fd/N
special filenames if your OS supports them:
#!/usr/bin/env python2
import sys
from subprocess import Popen, PIPE
prog1 = Popen([sys.executable, "prog1.py", "input_1.txt"], stdout=PIPE)
with prog1.stdout:
cmd2 = [sys.executable, "prog2.py", "/dev/fd/%d" % prog1.stdout.fileno()]
prog2 = Popen(cmd2, stdout=PIPE)
output = prog2.communicate()[0]
statuses = [prog1.wait(), prog2.returncode]
You should check whether prog2
supports a special filename '-'
to read the input from stdin i.e., to emulate the pipeline: prog1 input_1.txt | prog2 -
:
#!/usr/bin/env python
import sys
from subprocess import Popen, PIPE
prog2 = Popen([sys.executable, "prog2.py", "-"], stdin=PIPE, stdout=PIPE)
prog1 = Popen([sys.executable, "prog1.py", "input_1.txt"], stdout=prog2.stdin)
output = prog2.communicate()[0]
statuses = [prog1.wait(), prog2.returncode]
Or (better) import the modules instead of running them as subprocesses and run the corresponding functions, use multiprocessing if necessary. See Call python script with input with in a python script using subprocess