手动关闭子流程 [英] Manually closing subprocess.PIPE

查看:77
本文介绍了手动关闭子流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

std = subprocess.PIPE 

并检查输出的每一行中的特定术语.如果得到完成",则通过os.kill手动终止子进程:

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)

该进程确实被杀死(我在Unix中使用'ps'命令进行了检查),但是最后我得到一个错误:

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:

我认为这很可能是因为我在没有真正关闭PIPE的情况下终止了该进程.有什么建议可以手动完成吗?

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?

推荐答案

Popen对象具有

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

如果您真的想用SIGINT杀死,还有一个send_signal方法.

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

这篇关于手动关闭子流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆