python 兩種多執行緒比較,有時多執行緒有可能變成累贅
阿新 • • 發佈:2020-12-13
首先我是為了把這56w左右的資料清洗
變成這樣:
從一個txt清洗,寫到另一個txt中。 原本是幾千條資料 ,一直用的普通的,速度還挺快,今天想清洗這56w資料,就想到了多執行緒 。
第一種方法:
def huoqu(file): ts_queue = Queue(10000) with open(file, 'r')as f: t = f.read() IP = t.split('\n') for i in IP: ts_queue.put(i) return ts_queue def qingxi(ts_queue):while not ts_queue.empty(): i = ts_queue.get() port_1 = re.findall(r"W12.*", i) port_1 = ''.join(port_1) try: t = zidian.zi_dian() port = str(t[port_1]) except: port = '9999' port_2 = re.findall(r"/common.*", i) port_2= ''.join(port_2) IP = i.replace(port_2, port) with open('IP3.txt', 'a+')as g: g.write(IP) g.write('\n') with open('IP.txt','r')as f: t= f.read() IP = t.split('\n') heji = [] for i in IP: port_1 = re.findall(r"W12.*", i) port_1= ''.join(port_1) try: t = zidian.zi_dian() port = str(t[port_1]) except: port = '9999' port_2 = re.findall(r"/common.*", i) port_2 = ''.join(port_2) IP = i.replace(port_2, port) heji.append(IP) #print(IP) heji.pop() for i in heji: with open('IP2.txt', 'a+')as g: g.write(i) g.write('\n') if __name__ == "__main__": start = datetime.datetime.now().replace(microsecond=0) print('開始————————讀取列表:') t = 'IP.txt' s = huoqu(t) threads = [] for i in range(100): t = threading.Thread(target=qingxi, name='th-' + str(i), kwargs={'ts_queue': s}) threads.append(t) for t in threads: t.start() for t in threads: t.join() end = datetime.datetime.now().replace(microsecond=0) print('刪除耗時:' + str(end - start))
這是平常我最喜歡用的多執行緒方法,非阻塞式得,速度最快的,但今天卡死了,不動了,原因
return ts_queue
需要新增56w的資料加入,來進行執行緒運作,當時不行了。
不新增執行緒還能運作,就是很慢,這個完全不工作了、
想到了換下一種,並且運用了自己帶函式 map() 來提高效率。
:
def square(x): port_1 = re.findall(r"W12.*", x) port_1 = ''.join(port_1) try: t = zidian.zi_dian() port = str(t[port_1]) except: port = '9999' port_2 = re.findall(r"/common.*", x) port_2 = ''.join(port_2) IP = x.replace(port_2, port) return IP def main(): with open('IP.txt', 'r')as f: t = f.read() IP = t.split('\n') IP.pop() res = map(square, IP) t_list = [] for ip_port in res: t = threading.Thread(target=is_enable, args=(ip_port,)) t.start() t_list.append(t) for t in t_list: t.join() def is_enable(ip_port): with open('IP3.txt', 'a+')as g: g.write(ip_port) g.write('\n') if __name__ == '__main__': start = datetime.datetime.now().replace(microsecond=0) main() end = datetime.datetime.now().replace(microsecond=0) print('刪除耗時:' + str(end - start)) #刪除耗時:0:05:14
並換上了另一種快速的多執行緒,清洗用內建函式完成,寫入檔案用多執行緒,但居然用了5分多種,多了幾個for迴圈,大大拉低了速度,這就說明這個完全沒必要用多執行緒,還拉低了速度。
這時候看下不用多執行緒的。:
def square(x): port_1 = re.findall(r"W12.*", x) port_1 = ''.join(port_1) try: t = zidian.zi_dian() port = str(t[port_1]) except: port = '9999' port_2 = re.findall(r"/common.*", x) port_2 = ''.join(port_2) IP = x.replace(port_2, port) return IP start = datetime.datetime.now().replace(microsecond=0) with open('IP.txt', 'r')as f: t = f.read() IP = t.split('\n') IP.pop() res = map(square, IP) for i in res: with open('IP3.txt', 'a+')as g: g.write(i) g.write('\n') end = datetime.datetime.now().replace(microsecond=0) print('刪除耗時:' + str(end - start)) # 刪除耗時:0:03:52
明顯快多了,只用4分鐘左右,顯然for 迴圈在56w資料面前,大大拉低了速度,耗費了時間,所以兩種多執行緒個有優點,當資料過大,寫入檔案不如 不用多執行緒。
要想加快,可以把列表分成幾個,單獨給每個列表寫入檔案,但順序會發生變化,更加吃電腦配置了。