問(wèn)題產(chǎn)生描述
使用子進(jìn)程處理一個(gè)大的日志文件,并對(duì)文件進(jìn)行分析查詢,需要等待子進(jìn)程執(zhí)行的輸出結(jié)果,進(jìn)行下一步處理。
出問(wèn)題的代碼
# 啟用子進(jìn)程執(zhí)行外部shell命令def __subprocess(self,cmd): try: # 執(zhí)行外部shell命令, 輸出結(jié)果輸出管道 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p.wait() # 從標(biāo)準(zhǔn)輸出讀出shell命令的輸出結(jié)果 #rt = p.stdout.read().decode() # 以換行符拆分?jǐn)?shù)據(jù),并去掉換行符號(hào)存入列表 rt_list = rt.strip().split('/n') except Exception as e: if(DEBUG): print(traceback.format_exc()) return rt_list問(wèn)題分析
子進(jìn)程產(chǎn)生一些數(shù)據(jù),他們會(huì)被buffer起來(lái),當(dāng)buffer滿了,會(huì)寫到子進(jìn)程的標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出,這些東西通過(guò)管道發(fā)送給父進(jìn)程。當(dāng)管道滿了之后,子進(jìn)程就停止寫入,于是就卡住了,及時(shí)取走管道的輸出就不會(huì)出現(xiàn)阻塞了
但是本人此處采取的是臨時(shí)文件接收子進(jìn)程輸出,由于臨時(shí)文件是建立在磁盤上的,沒(méi)有size的限制,并且文件被close后,相應(yīng)的磁盤上的空間也會(huì)被釋放掉。
已改進(jìn)的代碼
import tempfile# 啟用子進(jìn)程執(zhí)行外部shell命令def __subprocess(self,cmd): try: # 得到一個(gè)臨時(shí)文件對(duì)象, 調(diào)用close后,此文件從磁盤刪除 out_temp = tempfile.TemporaryFile(mode='w+') # 獲取臨時(shí)文件的文件號(hào) fileno = out_temp.fileno() # 執(zhí)行外部shell命令, 輸出結(jié)果存入臨時(shí)文件中 p = subprocess.Popen(cmd, shell=True, stdout=fileno, stderr=fileno) p.wait() # 從臨時(shí)文件讀出shell命令的輸出結(jié)果 out_temp.seek(0) rt = out_temp.read() # 以換行符拆分?jǐn)?shù)據(jù),并去掉換行符號(hào)存入列表 rt_list = rt.strip().split('/n') except Exception as e: if(DEBUG): print(traceback.format_exc()) finally: if out_temp: out_temp.close() return rt_list以上這篇對(duì)Python subprocess.Popen子進(jìn)程管道阻塞詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選