Phil O
Phil O

Reputation: 1668

Understanding python subprocess popen asynchronous

My understanding is that subprocess.popen is an asynchronous call and appending .wait() to the call will make it synchronous. Will the second of these popen calls execute after the first call completes?

proc1 = subprocess.Popen(first_command, stdout=subprocess.PIPE, shell=True)
proc2 = subprocess.Popen(second_command, stdin=proc1.stdout, stdout=self.fw, shell=True)

I'm trying to determine when it's necessary to use wait() and why it causes errors when used in the above example popen statements, for example:

proc1 = subprocess.Popen(first_command, stdout=subprocess.PIPE, shell=True).wait()  # throws exception
proc2 = subprocess.Popen(second_command, stdin=proc1.stdout, stdout=self.fw, shell=True).wait()  # seems ok

Upvotes: 3

Views: 2218

Answers (1)

Phil O
Phil O

Reputation: 1668

After lots of trial and error and re-reading other posts and the docs, this is what works.

proc1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)
# don't put wait here because of potential deadlock if stdout buffer gets full and we're waiting for proc2 to consume buffer then we're deadlocked
proc2 = subprocess.Popen(cmd2, stdin=proc1.stdout, stdout=self.fw, shell=True)
# ok to wait here
proc2.wait()
# ok to test return code after proc2 completes
if proc2.returncode != 0:
    print('Error spawning cmd2')
else:
    print('Success spawning cmd2')

Hopefully this will help someone else.

Upvotes: 2

Related Questions