2012-10-23 54 views
1

我使用手動關閉subprocess.PIPE

std = subprocess.PIPE 

和檢查在output.If我得到「做」的每一行特定的詞,我手動殺由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中的「S」命令),但最後我得到一個錯誤:

close failed in file object destructor: 
Error in sys.excepthook: 
Original exception was: 

我認爲很可能是因爲我沒有真正關閉PIPE而殺死了這個過程。任何建議如何我可以手動做到這一點?

+0

'std.close()'? – cdarke

+0

@cdarke'std'是'subprocess.PIPE',它只是'-1'(或它是'-2'?),它沒有'.close()'... – glglgl

回答

1

Popen對象有一個terminate方法意味着要做到這一點。你不能用這個方法嗎?

如果你真的想用SIGINT殺死,還有一個send_signal方法。

+0

,奇怪的是以下都不是命令似乎工作:p.kill(),p.terminate()。它表示沒有名爲'kill'的對象。但是,我得到了解決方案。我用SIGTERM而不是SIGINT。這也關閉了PIPE。 無論如何,謝謝。 –

+0

在Python 2.6中引入了'kill'和'terminate'方法。看來你使用的是舊版本。 –

+0

@RolandSmith雅,就是這一點。我正在使用python 2.6.7,仍然 給出的錯誤:AttributeError:'Popen'對象沒有屬性'kill' –