0
votes

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...
1
I don't get it. Do you really create threads? Because if you do you can loop and wait in each thread (that's what I would do, and I would even use check_output instead of Popen in your case (BTW since f is open in your loop, each new result overwrites the previous one) - Jean-François Fabre
if you create threads, we're not seeing the code. Are you mixing up Popen with threads? because Popen creates processes. - Jean-François Fabre

1 Answers

0
votes

Right now you have a thread launching jobs for all folders. You need to invert that to one thread per folder launching all of its jobs. That's easy to do with a ThreadPool, which will handle the thread start / queueing operations for you.

import multiprocessing.pool

jobs = [<...testjob1.py...test parameters>,
    <...testjob2.py...test parameters>,
    <...testjob3.py...test parameters>]

def worker(folder, jobs, outfp):
    for job in jobs:
        subprocess.call(job, cwd=folder, stdout=outfp))

folder_list = next(os.walk(parent))[1]
pool = multiprocessing.pool.ThreadPool(len(folder_list))
for result in pool.map(worker, ((folder, jobs, f) for folder in folder_list))):
    pass
pool.close()
pool.join()

If its okay to have multiple jobs running in the same folder at the same time, you can schedule them more fine grained. Suppose the job in one directory is inherently longer than other directories. In the example above, you'd wait for the "tall poll". In this example, jobs would spread out more evenly.

import multiprocessing.pool

jobs = [<...testjob1.py...test parameters>,
    <...testjob2.py...test parameters>,
    <...testjob3.py...test parameters>]

def worker(folder, jobs, outfp):
    subprocess.call(job, cwd=folder, stdout=outfp))

folder_list = next(os.walk(parent))[1]
pool = multiprocessing.pool.ThreadPool(len(folder_list))
for result in pool.map(worker, ((folder, job, f) 
        for folder , job in itertools.product(folder_list, jobs))),
        chunksize=1):
    pass
pool.close()
pool.join()