日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 两种多线程比较

發布時間:2025/3/20 python 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 两种多线程比较 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先我是為了把這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() 來提高效率。

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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數據面前,大大拉低了速度,耗費了時間,所以兩種多線程個有優點,當數據過大,寫入文件不如 不用多線程。

要想加快,可以把列表分成幾個,單獨給每個列表寫入文件,但順序會發生變化,更加吃電腦配置了。

總結

以上是生活随笔為你收集整理的python 两种多线程比较的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。