I'm working on a script in Python which open multiple subprocesses in this way:
for file in os.listdir(FOLDER):
subprocess.Popen(([myprocess]))
Now this processes could be 10-20 running in parallel, and each of them will output in the console a single string line. What I want to do is to append these outputs (no matter in which order) to an array, and when all the processes are done, continue with the script doing other stuff.
I've no idea how to append each output to the array, I was thinking that to check if all subprocesses are done I could do something like this:
outputs = []
k = len(os.listdir(FOLDER))
if len(outputs) == k
print "All processes are done!"
UPDATE! This code seems to work now:
pids=set()
outputs = []
for file in os.listdir(FOLDER):
p = subprocess.Popen(([args]), stdout=subprocess.PIPE)
pids.add(p.pid)
while pids:
pid,retval=os.wait()
output = p.stdout.read()
outputs.append(output)
print('{p} finished'.format(p=pid))
pids.remove(pid)
print "Done!"
print outputs
The problem is that outputs
look like this
>> Done!
>> ['OUTPUT1', '', '', '', '', '', '', '', '', '']
Only the first value is filled, the others are left empty, why?