简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM