Manually closing subprocess.PIPE

2019-07-25 09:33发布

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?

标签: python kill
1条回答
Ridiculous、
2楼-- · 2019-07-25 10:02

The Popen object has a terminate method meant to do this. Could you not use this method instead?

If you really want to kill with SIGINT there is also a send_signal method.

查看更多
登录 后发表回答