1. 程式人生 > >concurrent.futures模塊

concurrent.futures模塊

繼續 循環 方法 nbsp call col 並不會 clas adp

1.concurrent.futures模塊介紹

2.ThreadPoolExecutor線程池使用

3.ProcessPoolExecutor進程池使用

1.concurrent.futures模塊介紹

 1 # 介紹
 2     concurrent.futures模塊提供了高度封裝的異步調用接口
 3     ThreadPoolExecutor:線程池,提供異步調用
 4     ProcessPoolExecutor: 進程池,提供異步調用
 5 
 6 # 基本方法
 7 # submit(fn, *args, **kwargs)  異步提交任務
 8 
 9 # map(func, *iterables, timeout=None, chunksize=1)  取代for循環submit的操作
10 11 # shutdown(wait=True) 相當於進程池的pool.close()+pool.join()操作 12 wait=True,等待池內所有任務執行完畢回收完資源後才繼續 13 wait=False,立即返回,並不會等待池內的任務執行完畢 14 但不管wait參數為何值,整個程序都會等到所有任務執行完畢 15 submit和map必須在shutdown之前 16 17 # result(timeout=None) 取得結果 18 19 # add_done_callback(fn) 回調函數

2.ThreadPoolExecutor線程池使用

3.ProcessPoolExecutor進程池使用

concurrent.futures模塊