Manually closing subprocess.PIPE

一个人想着一个人 提交于 2019-12-11 10:18:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13030162/manually-closing-subprocess-pipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!