SaAtomic
SaAtomic

Reputation: 749

Don't wait for subprocesses, but check for returncode later on

I'm working on a script that runs a number of subprocesses. The script should be able to run them all, then check in a loop if they're done.

Not start one, wait for it to finish, then start the next.

For example:

sp1 = subprocess.Popen(["sleep 60", shell=True)
sp2 = subprocess.Popen(["sleep 10", shell=True)
sp3 = subprocess.Popen(["sleep 80", shell=True)

sp1_done = False
sp2_done = False
sp3_done = False
while not sp1_done and not sp2_done and not sp3_done:
    if sp1.returncode and not sp1_done:
        print("sp1 done")
        sp1_done = True
    if sp2.returncode and not sp2_done:
        print("sp2 done")
        sp2_done = True
    if sp3.returncode and not sp3_done:
        print("sp3 done")
        sp3_done = True

print("all done")

I've read that one can access the error code of the subprocess with

sp1_data = sp1.communicate()[0] and then with

returncode = sp1.returncode

But like this, the script waits for sp1 to finish.

How can I run multiple subprocesses, not wait for them to finish, but check if they're done (e.g. by checking the return code)?

Upvotes: 0

Views: 793

Answers (1)

John F
John F

Reputation: 186

You need to use the poll method for each subprocess started. And check returncode is not None:

 while ....  # as written
     sp1.poll()
     sp2.poll()
     sp3.poll()
     if sp1.returncode is not None and not sp1_done:
          .... # as written
     ... # as written

Upvotes: 1

Related Questions