First of all, you can indeed use subprocess.Popen
to start background processes and wait for their completion. See the documentation, specifically, the poll()
method. Run the UI event loop until the process has exited.
Second, it is usually a good idea to avoid threads in Python. The multiprocessing module is mostly useful when you want to parallelize tasks written in Python. IMO, I think it is easier to use the subprocess module if you are just launching external child processes.
The following pseudocode illustrates the idea:
experiments = [...]
process = None
def start_next_experiment():
if not experiments:
print "Done!"
else:
experiment = experiments.pop()
process = subprocess.Popen(experiment)
def on_start_clicked():
start_next_experiment()
def on_stop_clicked():
# Clear the queue
experiments = []
# optional: Kill the process
if process:
process.terminate()
def on_idle():
if process:
# use e.g. a PyQT timer to run this method periodically
process.poll()
if process.returncode is not None:
process = None
start_next_experiment()