python subprocess: raising an error when a process prompts
I have a python script that runs on a server after hours and invokes many shell subprocesses. None of the programs that are called should be prompting, but sometimes it happens and the script hangs, waiting for input until the user (me) notices and gets angry. :)
Tried: Using p.communicate() with stdin=PIPE, as written in the python subprocess documentation.
Running: Ubuntu 10.10, Python 2.6
I don't want to respond to the prompts, I want the script to raise an error and continue. Any thoughts?
Thanks, Alexa开发者_JAVA技巧nder.
As a catch-all solution to any problems in subprocesses I'd recommend using timeouts for all shell calls. There's no built-in timeout support in subprocess
module calls, so you need to use signals. See details here: Using module 'subprocess' with timeout
You need a time-out while waiting for your tasks to complete and then have your script kill or terminate the process (in addition to raising the error).
Pyexpect is a Python tool for dealing with subprocesses that may generate output (and may need input as a result). It will help you easily deal with the various cases, including managing timeouts.
See: http://www.noah.org/wiki/pexpect
精彩评论