I'm trying to automate a manual simulation test using python on shell. I have my data setup under subfolders within a parent directory like so:
/vol/parent/child1
/vol/parent/child2
/vol/parent/child3
...
/vol/parent/childn
I have separate scripts, which run a test job (which is also a python program) in each of these child directories. The scripts use subprocess.Popen and an example is as follows:
folder_list = next(os.walk(parent))[1]
for child in folder_list:
f = open("/vol/testjob1/nohup.out", "w")
p = subprocess.Popen([<...testjob1.py...test parameters>], cwd = '/vol01/testjob1/', stdout=f)
This runs the job on each of these child folders, 1 job per thread.
Usually, I wait for all the n jobs to finish and then run my next test (say testjob2), which is again a similar subprocess type program.
I'm trying to combine all of these subprocess test scripts into 1 master script. And the main goal is that the moment a job finishes on 1 thread, the next subprocess begins on that thread. I tried appending p.wait() at the end, but that means it's going to wait for every thread to finish before starting the next thread (which kills the purpose of running these jobs in parallel).
How can I use a combination of subprocess.popen, p.wait() and for loops to make sure 1 process starts after another, like so:
(child1)| thread1: Subprocess1 (done)--> start subprocess2 (done)--> start subprocess3...
(child2)| thread2: Subprocess1 (done)--> start subprocess2 (done)--> start subprocess3...
...
(childn)| threadn: Subprocess1 (done)--> start subprocess2 (done)--> start subprocess3...
check_outputinstead ofPopenin your case (BTW sincefis open in your loop, each new result overwrites the previous one) - Jean-François FabrePopenwith threads? becausePopencreates processes. - Jean-François Fabre