llvmstacker
llvmstacker

Reputation: 9

Python Subprocess communicate fails after terminate

I'm experiencing an issue where a call to proc.communicate() still hangs even after calling proc.terminate().

I create tasks to be run in the background with the call

import subprocess as sub
p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, shell=False)

At the completion of the script, I call terminate(), communicate(), and wait() to gather information from the process.

p.terminate()
errorcode = p.wait()
(pout, perr) = p.communicate()

The script hangs at the call to communicate. I'd assumed that any call to communicate that follows a call to terminate would return immediately. Is there any reason why this would fail?

Edit: I'm using this method because the command is really a tight loop that won't terminate on its own. I'd like to use p.terminate() to do that, and then see what the stdout and stderr has to offer.

Upvotes: 0

Views: 927

Answers (1)

Athena
Athena

Reputation: 3228

You don't need the first two statements. Just call communicate().

Upvotes: 1

Related Questions