简体   繁体   中英

Python subprocess package does not report errors from subprocess?

On some machine, for the following piece of code

p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT);
out, err = p.communicate()

This script would just hang at p.communicate() and does not return.

After I manually run the command, I finally see the error messages.

Why is this, and how should I solve it?

Thanks.

尝试stderr = subprocess.PIPE不是stderr = subprocess.STDOUT

I guess your program never ends?

when you call communicate() it does different things under different os. But it always waits for the launched process to exit on its own eg. calls p.wait(). p.wait() terminates only if the rocess terminates.

Solutions:

  • You could copy the source code of subprocess.Popen._communicate and alter it so it does not use wait() but time.sleep and some timeout
  • You write your own code that reads stdout and stderr and stops if the program outputs too much stderr
  • You change the main() function of the file that never ands this way

way

def main():
    ## here is you program code

if __name__ == '__main__':
    import thread, sys, time
    _id = thread.start_new(main, ())
    time.sleep(1)
    t = time.time() + 100 # execute maximum 100 + 1 seconds
    while t > time.time() and _id in sys._current_frames():
        time.sleep(0.001)

If you want to look at the source of subprocess you can find it at subprocess.__file__ .

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