I want to run multiple threads simultaneously, and wait until all of them are done before continuing.
import subprocess
# I want these to happen simultaneously:
subprocess.call(scriptA + argumentsA)
subprocess.call(scriptA + argumentsB)
subprocess.call(scriptA + argumentsC)
# I want to wait until the above threads are all finished, and then run this:
print("All threads are done.")
I tried to use threading like the example here:
from threading import Thread
import subprocess
def call_script(args)
subprocess.call(args)
t1 = Thread(target=call_script, args=(scriptA + argumentsA))
t2 = Thread(target=call_script, args=(scriptA + argumentsB))
t3 = Thread(target=call_script, args=(scriptA + argumentsC))
t1.start()
t2.start()
t3.start()
# TODO: Wait for all threads to finish.
print("All threads are done.")
How do I wait for the threads to finish before running the last line?
You need to use join method of
Threadobject in the end of the script.Thus the main thread will wait till
t1,t2andt3finish execution.