I am using
std = subprocess.PIPE
and checking a particular term in every line of the output.If I get 'done', I manually kill the subprocess by os.kill :
p = subprocess.Popen(['vprobe' ,'/vprobe/myhello.emt'], shell = False, stdout=subprocess.PIPE, preexec_fn=os.setsid)
while True:
line = p.stdout.readline()
logfile.write(line)
if re.search("done", line):
break
print "waiting"
os.kill(p.pid, signal.SIGINT)
The process does get killed (i checked using the 'ps' command in unix) but at the end I get an error that:
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
which I think is most probably because I have killed the process without actually closing the PIPE. Any suggestions how I can manually do it?
The
Popen
object has aterminate
method meant to do this. Could you not use this method instead?If you really want to kill with
SIGINT
there is also asend_signal
method.