python 實(shí)現(xiàn)多線程的三種方法總結(jié)
1._thread.start_new_thread(了解)
import threading import time import _thread def job(): print("這是一個需要執(zhí)行的任務(wù)。。。。。") print("當(dāng)前線程的個數(shù):", threading.active_count() ) print("當(dāng)前線程的信息:", threading.current_thread()) time.sleep(100) if __name__ == '__main__': # 創(chuàng)建多線程時, 需要制定該線程執(zhí)行的任務(wù) _thread.start_new_thread(job, ()) _thread.start_new_thread(job, ()) job()
2.threading.Thread
import threading import time def job(): print("這是一個需要執(zhí)行的任務(wù)。。。。。") print("當(dāng)前線程的個數(shù):", threading.active_count() ) time.sleep(1) print("當(dāng)前線程的信息:", threading.current_thread()) if __name__ == '__main__': # 創(chuàng)建多線程時, 需要制定該線程執(zhí)行的任務(wù).name線程名字 target目標(biāo)函數(shù)名 t1 = threading.Thread(target=job,name='job1') t2 = threading.Thread(target=job,name='job2') t1.start() t2.start() print(threading.active_count()) print("程序執(zhí)行結(jié)束.....")
輸出:
這是一個需要執(zhí)行的任務(wù)。。。。。
當(dāng)前線程的個數(shù): 3
這是一個需要執(zhí)行的任務(wù)。。。。。
3
程序執(zhí)行結(jié)束.....
當(dāng)前線程的個數(shù): 3
當(dāng)前線程的信息: <Thread(job1, started 140416648140544)>
當(dāng)前線程的信息: <Thread(job2, started 140416639747840)>
出現(xiàn)的問題: 主線程執(zhí)行結(jié)束, 但是子線程還在運(yùn)行。
join()方法可以等待所有的子線程執(zhí)行結(jié)束之后, 再執(zhí)行主線程。
import threading import time def job(): print("這是一個需要執(zhí)行的任務(wù)。。。。。") print("當(dāng)前線程的個數(shù):", threading.active_count() ) print("當(dāng)前線程的信息:", threading.current_thread()) time.sleep(1) if __name__ == '__main__': # 創(chuàng)建多線程時, 需要制定該線程執(zhí)行的任務(wù).name線程名字 target目標(biāo)函數(shù)名 t1 = threading.Thread(target=job,name='job1') t2 = threading.Thread(target=job,name='job2') t1.start() t2.start() print(threading.active_count()) # 出現(xiàn)的問題: 主線程執(zhí)行結(jié)束, 但是子線程還在運(yùn)行。 # 等待所有的子線程執(zhí)行結(jié)束之后, 再執(zhí)行主線程 t1.join() t2.join() print("程序執(zhí)行結(jié)束.....")
之前寫過一個簡單爬蟲的實(shí)驗,現(xiàn)在希望獲取十個ip的城市和國家
-不使用多線程
import time from urllib.request import urlopen # 記錄時間的裝飾器 def timeit(f): def wrapper(*args, **kwargs): start_time = time.time() res = f(*args, **kwargs) end_time = time.time() print("%s函數(shù)運(yùn)行時間:%.2f" % (f.__name__, end_time - start_time)) return res return wrapper def get_addr(ip): url = "http://ip-api.com/json/%s" % (ip) urlObj = urlopen(url) # 服務(wù)端返回的頁面信息, 此處為字符串類型 pageContent = urlObj.read().decode('utf-8') # 2. 處理Json數(shù)據(jù) import json # 解碼: 將json數(shù)據(jù)格式解碼為python可以識別的對象; dict_data = json.loads(pageContent) print(""" %s 所在城市: %s 所在國家: %s """ % (ip, dict_data['city'], dict_data['country'])) @timeit def main(): ips = ['12.13.14.%s' % (i + 1) for i in range(10)] for ip in ips: get_addr(ip) if __name__ == '__main__': main()
時間需要138.91秒。
-使用多線程
import threading import time from urllib.request import urlopen def timeit(f): def wrapper(*args, **kwargs): start_time = time.time() res = f(*args, **kwargs) end_time = time.time() print("%s函數(shù)運(yùn)行時間:%.2f" % (f.__name__, end_time - start_time)) return res return wrapper def get_addr(ip): url = "http://ip-api.com/json/%s" % (ip) urlObj = urlopen(url) # 服務(wù)端返回的頁面信息, 此處為字符串類型 pageContent = urlObj.read().decode('utf-8') # 2. 處理Json數(shù)據(jù) import json # 解碼: 將json數(shù)據(jù)格式解碼為python可以識別的對象; dict_data = json.loads(pageContent) print(""" %s 所在城市: %s 所在國家: %s """ % (ip, dict_data['city'], dict_data['country'])) @timeit def main(): ips = ['12.13.14.%s' % (i + 1) for i in range(10)] threads = [] for ip in ips: # 實(shí)例化10個對象,target=目標(biāo)函數(shù)名,args=目標(biāo)函數(shù)參數(shù)(元組格式) t = threading.Thread(target=get_addr, args=(ip, )) threads.append(t) t.start() # 等待所有子線程結(jié)束再運(yùn)行主線程 [thread.join() for thread in threads] if __name__ == '__main__': main()
3.重寫run方法
重寫run方法, 實(shí)現(xiàn)多線程, 因為start方法執(zhí)行時, 調(diào)用的是run方法;run方法里面編寫的內(nèi)容就是你要執(zhí)行的任務(wù);
import threading import time # 重寫一個類,繼承于threading.Thread class MyThread(threading.Thread): def __init__(self, jobName): super(MyThread, self).__init__() self.jobName = jobName # 重寫run方法, 實(shí)現(xiàn)多線程, 因為start方法執(zhí)行時, 調(diào)用的是run方法; # run方法里面編寫的內(nèi)容就是你要執(zhí)行的任務(wù); def run(self): print("這是一個需要執(zhí)行的任務(wù)%s。。。。。" %(self.jobName)) print("當(dāng)前線程的個數(shù):", threading.active_count() ) time.sleep(1) print("當(dāng)前線程的信息:", threading.current_thread()) if __name__ == '__main__': t1 = MyThread("name1") t2 = MyThread("name2") t1.start() t2.start() t1.join() t2.join() print("程序執(zhí)行結(jié)束.....")
重寫run方法實(shí)現(xiàn)剛才爬蟲多線程案例
import threading import time from urllib.request import urlopen def timeit(f): def wrapper(*args, **kwargs): start_time = time.time() res = f(*args, **kwargs) end_time = time.time() print("%s函數(shù)運(yùn)行時間:%.2f" % (f.__name__, end_time - start_time)) return res return wrapper class MyThread(threading.Thread): def __init__(self, ip): super(MyThread, self).__init__() self.ip = ip def run(self): url = "http://ip-api.com/json/%s" % (self.ip) urlObj = urlopen(url) # 服務(wù)端返回的頁面信息, 此處為字符串類型 pageContent = urlObj.read().decode('utf-8') # 2. 處理Json數(shù)據(jù) import json # 解碼: 將json數(shù)據(jù)格式解碼為python可以識別的對象; dict_data = json.loads(pageContent) print(""" %s 所在城市: %s 所在國家: %s """ % (self.ip, dict_data['city'], dict_data['country'])) @timeit def main(): ips = ['12.13.14.%s' % (i + 1) for i in range(10)] threads = [] for ip in ips: # 實(shí)例化自己重寫的類 t = MyThread(ip) threads.append(t) t.start() [thread.join() for thread in threads] if __name__ == '__main__': main()
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
python使用ctypes調(diào)用dll遇到的坑解決記錄
這篇文章主要為大家介紹了python使用ctypes調(diào)用dll遇到的坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12用Python中的__slots__緩存資源以節(jié)省內(nèi)存開銷的方法
這篇文章主要介紹了用Python中的__slots__通過緩存資源的方式以節(jié)省內(nèi)存開銷的方法,且示例代碼非常簡單,需要的朋友可以參考下2015-04-04python3中datetime庫,time庫以及pandas中的時間函數(shù)區(qū)別與詳解
這篇文章主要介紹了python3中datetime庫,time庫以及pandas中的時間函數(shù)區(qū)別與詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python實(shí)現(xiàn)Logger打印功能的方法詳解
最近工作中遇到了打印的需求,通過查找相關(guān)的資料發(fā)現(xiàn)Python中Logger可以很好的實(shí)現(xiàn)打印,所以下面這篇文章主要給大家介紹了關(guān)于Python如何實(shí)現(xiàn)Logger打印功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09