Synbitz Prowduczions
Synbitz Prowduczions

Reputation: 387

Wait until nested python script ends before continuing in current python script

I have a python script that calls another python script. Inside the other python script it spawns some threads.How do I make the calling script wait until the called script is completely done running?

This is my code :

while(len(mProfiles) < num):
        print distro + " " + str(len(mProfiles))
        mod_scanProfiles.main(distro)
        time.sleep(180)
        mProfiles = readProfiles(mFile,num,distro)
        print "yoyo"

How do I wait until mod_scanProfiles.main() and all threads are completely finished? ( I used time.sleep(180) for now but its not good programming habit)

Upvotes: 5

Views: 4016

Answers (1)

stderr
stderr

Reputation: 8722

You want to modify the code in mod_scanProfiles.main to block until all it's threads are finished.

Assuming you make a call to subprocess.Popen in that function just do:

# in mod_scanPfiles.main:
p = subprocess.Popen(...)
p.wait() # wait until the process completes.

If you're not currently waiting for your threads to end you'll also want to call Thread.join (docs) to wait for them to complete. For example:

# assuming you have a list of thread objects somewhere
threads = [MyThread(), ...]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

Upvotes: 7

Related Questions