Waiting for two subprocesses to finish but not necessarily waiting for first

178 views Asked by At

I am writing a program that creates two new processes and must wait for them both to finish before continuing. How does one launch both processes and have the program wait for both to exit? Consider the pseudocode:

I currently have:

create_process("program1.exe").wait()
create_process("program2.exe").wait()

This is enificent as program2 can run concurently with program1.

create_process("program1.exe")
create_process("program2.exe").wait()

This may be wrong as program1 may take longer than program2.

I'm interested in a general solution, I bet there's algorithms or design patterns invented to deal with this stuff. But to add context to the question, I'm writing a Python script that calls pgsql2shp.exe twice, to export two tables from the database to the local machine and then preform an intersection. This script is written in Python 2.7 and uses subprocess.popen

1

There are 1 answers

1
Ray Perea On

How about using Threading? If you spin up a couple of threads, each thread can run independently and you can join the threads when they are completed.

Try some code like this: (This code is heavily commented so that you can follow what's going on)

# Import threading
import threading

# Create a handler class.
# Each instance will run in it's own independent thread
class ThreadedHandler (threading.Thread):

    # This method is called when you call threadInstance.start()
    def run(self):

        # Run your sub process and wait for it
        # How you run your process is up to you
        create_process(self.programName).wait()

# Create a new thread object
thread1 = ThreadedHandler()

# Set your program name so that when the thread is started
# the correct process is run
thread1.programName = 'program1.exe'

# Start the thread
thread1.start()

# Again, create a new thread object for the 2nd program
thread2 = ThreadedHandler()

# Set the program name
thread2.programName = 'program2.exe'

# Start the thread
thread2.start()

# At this point, each program is running independently in separate threads
# Each thread will wait for their respective sub process to complete

# Here, we join both threads. (Wait for both threads to complete)
thread1.join()
thread2.join()

# When we get here, both of our programs are finished and they both ran in parallel