1. 程式人生 > >multiprocessing vs concurrent.futures

multiprocessing vs concurrent.futures

背景

Python中提供了兩個模組來簡化多執行緒/程序的處理,concurrent.futures、multiprocessing這兩個模組是使用最多的。那麼這兩個模組究竟有什麼差別。concurrent.futures是在python3.2中引入的,提供了一種遍歷的方式來管理非同步任務。

The concurrent.futures module provides a high-level interface for asynchronously executing callables.

The asynchronous execution can be performed with threads, using
ThreadPoolExecutor, or separate processes, using ProcessPoolExecutor. Both implement the same interface, which is defined by the abstract Executor class.

例子

from concurrent.futures import ProcessPoolExecutor
def pool_factorizer_map(nums, nprocs):
    # Let the executor divide the work among processes by using 'map'.
with ProcessPoolExecutor(max_workers=nprocs) as executor: return {num:factors for num, factors in zip(nums, executor.map(factorize_naive, nums))}
import multiprocessing as mp
def mp_factorizer_map(nums, nprocs):
    with
mp.Pool(nprocs) as pool: return {num:factors for num, factors in zip(nums, pool.map(factorize_naive, nums))}

比較

  1. 顯然用futures的寫法上更簡潔一些,concurrent.futures的效能並沒有更好,只是讓編碼變得更簡單。考慮併發程式設計的時候,任何簡化都是好事。從長遠來看,concurrent.futures編寫的程式碼更容易維護。
  2. 使用map時,future是逐個迭代提交,multiprocessing.Pool是批量提交jobs,因此對於大批量jobs的處理,multiprocessing.Pool效率會更高一些。對於需要長時間執行的作業,用future更佳,future提供了更多的功能(callback, check status, cancel)。
  3. concurrent.futures.ProcessPoolExecutor是對multiprocessing的封裝,在執行時需匯入__main__,不能直接在互動視窗工作。
  4. 由於GIL限制,建議:IO密集的任務,用ThreadPoolExecutor;CPU密集任務,用ProcessPoolExcutor

相關推薦

multiprocessing vs concurrent.futures

背景 Python中提供了兩個模組來簡化多執行緒/程序的處理,concurrent.futures、multiprocessing這兩個模組是使用最多的。那麼這兩個模組究竟有什麼差別。concurre

多進程 multiprocessing 多線程Threading 線程池和進程池concurrent.futures

不用 文件 進程池 lba ren 行操作 接收參數 出現 ali multiprocessing.procsess 定義一個函數 def func():pass 在if __name__=="__main__":中實例化 p = process(target=子進程要執

Python並發編程之線程池/進程池--concurrent.futures模塊

when nod 模式 進程 d參數 executor 其他 done 對比 h2 { color: #fff; background-color: #f7af0d; padding: 3px; margin: 10px 0px } 一、關於concurrent.futur

35、concurrent.futures模塊與協程

否則 ssp org 之間 內存 pat sta page hide concurrent.futures —Launching parallel tasks concurrent.futures模塊同時提供了進程池和線程池,它是將來的使用趨勢,同樣我們之前學習的進

python並發性能concurrent.futures

-1 兩個 size logs ssp int ces 底層 套接字 concurrent.futures模塊,可以利用multiprocessing實現真正的平行計算。核心原理是:concurrent.futures會以子進程的形式,平行的運行多個python解釋器,從而

Python3【模塊】concurrent.futures模塊,線程池進程池

tro containe them executor 進程池 自己的 from port clas   Python標準庫為我們提供了threading和multiprocessing模塊編寫相應的多線程/多進程代碼,但是當項目達到一定的規模,頻繁創建/銷毀進程或者線程是非

Python Day37 python多線程標準模塊concurrent.futures

ont imp syn multi true 提交 使用 bmi define 1 介紹 concurrent.futures模塊提供了高度封裝的異步調用接口 ThreadPoolExecutor:線程池,提供異步調用 ProcessPoolExecutor: 進程池,提供

python全棧開發基礎【第二十六篇】(concurrent.futures模塊、協程、Greenlet、Gevent)

會有 什麽 www 上一個 join 開發 tps 初始化 brush 註意 1.不能無限的開進程,不能無限的開線程最常用的就是開進程池,開線程池。其中回調函數非常重要回調函數其實可以作為一種編程思想,誰好了誰就去掉2.只要你用並發,就會有鎖的問題,但是你不能一直去自己加鎖

python--線程池(concurrent.futures)

time pass ever 表示 iterator may 檢測 列表 多參數 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # author:love_cat 4 5 # 為什麽需要線程池 6 #

python | concurrent.futures模塊提升數據處理速度

希望 數據預處理 exec resize 參考 margin 情況 neu folder p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; color: #454545 } p.p2 {

concurrent.futures模塊

繼續 循環 方法 nbsp call col 並不會 clas adp 1.concurrent.futures模塊介紹 2.ThreadPoolExecutor線程池使用 3.ProcessPoolExecutor進程池使用 1.concurrent.future

concurrent.futures效能

python因為其全域性直譯器鎖GIL而無法通過執行緒實現真正的平行計算。這個論斷我們不展開,但是有個概念我們要說明,IO密集型 vs. 計算密集型。 IO密集型:讀取檔案,讀取網路套接字頻繁。 計算密集型:大量消耗CPU的數學與邏輯運算,也就是我們這裡說的平行計算。 而concurre

concurrent.futures- 啟動並行任務

python因為其全域性直譯器鎖GIL而無法通過執行緒實現真正的平行計算。這個論斷我們不展開,但是有個概念我們要說明,IO密集型 vs. 計算密集型。 IO密集型:讀取檔案,讀取網路套接字頻繁。 計算密集型:大量消耗CPU的數學與邏輯運算,也就是我們這裡說的平行計算。 而concurrent.futur

Python標準模組--concurrent.futures模組(ThreadPoolExecutor:執行緒池,提供非同步呼叫、ProcessPoolExecutor: 程序池,提供非同步呼叫)

目錄 ProcessPoolExecutor: 程序池 ThreadPoolExecutor:執行緒池  map的用法  回撥函式 https://docs.python.org/dev/library/concurrent.futures.html

網路爬蟲必備知識之concurrent.futures庫 python究竟要不要使用多執行緒

就庫的範圍,個人認為網路爬蟲必備庫知識包括urllib、requests、re、BeautifulSoup、concurrent.futures,接下來將結對concurrent.futures庫的使用方法進行總結 建議閱讀本博的博友先閱讀下上篇部落格: python究竟要不要使用多執行緒,將會對co

concurrent.futures用法

submit   非同步提交 map(func, *iterables, timeout=None, chunksize=1)  

執行緒池 concurrent.futures

concurrent.futures 模組提供一個高級別介面給呼叫非同步執行 非同步執行可以用執行緒執行,使用 ThreadPoolExecutor,或者單獨的程序使用 ProcessPoolExecutor,兩者都實行了相同的介面,它由 抽象  Executor類定義 #

python 3 執行緒/程序池concurrent.futures模組使用

一、Executor和Future       concurrent.futures模組的基礎是Exectuor,Executor是一個抽象類,ThreadPoolExecutor和ProcessPoolExecutor是其非常有用的兩個子類。Future可以把它理

談談python concurrent.futures

Future 有幾個重要的方法: .done() 返回布林值,表示Future 是否已經執行 .add_done_callback() 這個方法只有一個引數,型別是可呼叫物件,Future執行結束後會回撥這個物件。 .result() 如果 Future 執行

python併發之concurrent.futures

concurrent:併發   Python標準庫為我們提供了threading和multiprocessing模組編寫相應的多執行緒/多程序程式碼。從Python3.2開始,標準庫為我們提供了concurrent.futures模組,它提供了ThreadPoolExecutor和ProcessPoolEx