Example of python subprocess

March 23, 2018

I needed to test out a thing with the subprocessing module and wrote an example of using it to run multiple processes in parallel, then reading all the stdouts and stderrs at the end:

import subprocess
import time

commands = ['./'] * 5
outputs = [None] * len(commands)
processes = [None] * len(commands)

start = time.time()

for i, command in enumerate(commands):
process = subprocess.Popen([command], stdout=subprocess.PIPE)
processes[i] = process

for i, process in enumerate(processes):
outputs[i] = process.communicate()
print(i, outputs[i])

print('elapsed seconds: ', time.time() - start)

sleep 2