您好,登錄后才能下訂單哦!
這篇文章主要介紹“python線程池的實(shí)現(xiàn)方法有哪些”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“python線程池的實(shí)現(xiàn)方法有哪些”文章能幫助大家解決問題。
一個(gè)程序運(yùn)行起來后,一定有一個(gè)執(zhí)行代碼的東西,這個(gè)東西就是線程;
一般計(jì)算(CPU)密集型任務(wù)適合多進(jìn)程,IO密集型任務(wù)適合多線程;
一個(gè)進(jìn)程可擁有多個(gè)并行的(concurrent)線程,當(dāng)中每一個(gè)線程,共享當(dāng)前進(jìn)程的資源
以下是對(duì)發(fā)現(xiàn)的幾種多線程進(jìn)行的匯總整理,均已測(cè)試運(yùn)行 多線程實(shí)現(xiàn)的四種方式分別是:
multiprocessing下面有兩種:
from multiprocessing.dummy import Pool as ThreadPool # 線程池 from multiprocessing.pool import ThreadPool # 線程池,用法無區(qū)別,唯一區(qū)別這個(gè)是線程池
另外兩種:
from concurrent.futures import ThreadPoolExecutor # python原生線程池,這個(gè)更主流 import threadpool # 線程池,需要 pip install threadpool,很早之前的
非阻塞方法
multiprocessing.dummy.Pool.apply_async() 和 multiprocessing.dummy.Pool.imap()
線程并發(fā)執(zhí)行
阻塞方法
multiprocessing.dummy.Pool.apply()和 multiprocessing.dummy.Pool.map()
線程順序執(zhí)行
from multiprocessing.dummy import Pool as Pool import time def func(msg): print('msg:', msg) time.sleep(2) print('end:') pool = Pool(processes=3) for i in range(1, 5): msg = 'hello %d' % (i) pool.apply_async(func, (msg,)) # 非阻塞,子線程有返回值 # pool.apply(func,(msg,)) # 阻塞,apply()源自內(nèi)建函數(shù),用于間接的調(diào)用函數(shù),并且按位置把元祖或字典作為參數(shù)傳入。子線程無返回值 # pool.imap(func,[msg,]) # 非阻塞, 注意與apply傳的參數(shù)的區(qū)別 無返回值 # pool.map(func, [msg, ]) # 阻塞 子線程無返回值 print('Mark~~~~~~~~~~~~~~~') pool.close() pool.join() # 調(diào)用join之前,先調(diào)用close函數(shù),否則會(huì)出錯(cuò)。執(zhí)行完close后不會(huì)有新的進(jìn)程加入到pool,join函數(shù)等待所有子進(jìn)程結(jié)束 print('sub-process done')
運(yùn)行結(jié)果:
from multiprocessing.pool import ThreadPool # 線程池,用法無區(qū)別,唯一區(qū)別這個(gè)是線程池 from multiprocessing.dummy import Pool as ThreadPool # 線程池 import os import time print("hi outside of main()") def hello(x): print("inside hello()") print("Proccess id: %s" %(os.getpid())) time.sleep(3) return x*x if __name__ == "__main__": p = ThreadPool(5) pool_output = p.map(hello, range(3)) print(pool_output)
運(yùn)行結(jié)果:
from concurrent.futures import ThreadPoolExecutor import threading import time # 定義一個(gè)準(zhǔn)備作為線程任務(wù)的函數(shù) def action(max): my_sum = 0 for i in range(max): print(threading.current_thread().name + ' ' + str(i)) my_sum += i return my_sum # 創(chuàng)建一個(gè)包含2條線程的線程池 pool = ThreadPoolExecutor(max_workers=2) # 向線程池提交一個(gè)task, 20會(huì)作為action()函數(shù)的參數(shù) future1 = pool.submit(action, 20) # 向線程池再提交一個(gè)task, 30會(huì)作為action()函數(shù)的參數(shù) future2 = pool.submit(action, 30) # 判斷future1代表的任務(wù)是否結(jié)束 print(future1.done()) time.sleep(3) # 判斷future2代表的任務(wù)是否結(jié)束 print(future2.done()) # 查看future1代表的任務(wù)返回的結(jié)果 print(future1.result()) # 查看future2代表的任務(wù)返回的結(jié)果 print(future2.result()) # 關(guān)閉線程池 pool.shutdown()
運(yùn)行結(jié)果:
需要 pip install threadpool
import threadpool def hello(m, n, o): """""" print("m = %s, n = %s, o = %s" % (m, n, o)) if __name__ == '__main__': # 方法1 # lst_vars_1 = ['1', '2', '3'] # lst_vars_2 = ['4', '5', '6'] # func_var = [(lst_vars_1, None), (lst_vars_2, None)] # 方法2 dict_vars_1 = {'m': '1', 'n': '2', 'o': '3'} dict_vars_2 = {'m': '4', 'n': '5', 'o': '6'} func_var = [(None, dict_vars_1), (None, dict_vars_2)] # 定義了一個(gè)線程池,表示最多可以創(chuàng)建poolsize這么多線程 pool = threadpool.ThreadPool(2) # 調(diào)用makeRequests創(chuàng)建了要開啟多線程的函數(shù),以及函數(shù)相關(guān)參數(shù)和回調(diào)函數(shù),其中回調(diào)函數(shù)可以不寫 requests = threadpool.makeRequests(hello, func_var) [pool.putRequest(req) for req in requests] # 將所有要運(yùn)行多線程的請(qǐng)求扔進(jìn)線程池 pool.wait() # 等待所有線程完成工作后退出 """ [pool.putRequest(req) for req in requests]等同于 for req in requests: pool.putRequest(req) """
運(yùn)行結(jié)果:
關(guān)于“python線程池的實(shí)現(xiàn)方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。