Skip to content

AI STUDIO Taiwan

台 灣 人 工 智 慧 與 資 料 科 學 研 究 室

  • About 關於本站
  • 專欄列表
  • Taiwan NLP.台灣自然語言處理與人工智慧交流社( 1.8萬位成員)
  • 關於 Taiwan NLP 社群

【Python 中階教學】多進程Pool.map的使用

Posted on 2021 年 7 月 20 日2023 年 7 月 20 日 By 楊 明翰 在〈【Python 中階教學】多進程Pool.map的使用〉中尚無留言
Python 中階
Print Friendly, PDF & Email

Pool.map語法

Pool.map 會阻塞(Blocking)主進程,直到所有子進程完成任務才會繼續往下執行,並且輸出結果也會按照原資料的順序排列,以下是語法:

Python
from multiprocessing import Pool # 引用進程池類
with Pool(進程數量) as pool: # 使用with做上下文管理
   結果=pool.map(函式,資料列表) # 使用map函式

這邊有一個簡單的迴圈的程式(example_no_multi.py)

Python
import time

data = [i for i in range(0, 16)]


def f(x):
    time.sleep(2)
    return x**2


data_out = [f(x) for x in data]
print("final result:")
print(data_out)

資料data經過函式f逐一處理後得到data_out,並輸出。現在我們希望用進程池,以map平行處理,便可改寫如下(example.py):

Python
from multiprocessing import Pool
import time

data = [i for i in range(0, 16)]


def f(x):
    time.sleep(2)
    return x**2


with Pool(16) as pool:
    data_out = pool.map(f, data)

print("final result:")
print(data_out)

這裡更動的地方只是開一個Pool出來,並將函式f和資料data作為參數傳入map函式。為了展示實際的運作時間和程序,以shell script 簡單了監測腳本(exe.sh):

ShellScript
#!/bin/bash
########################
# A Sample run monitor #
########################
delay=1
start=$(date +%s.%N)
cmd=$1
echo "execute command = $cmd"
eval $cmd &
echo 'running process:'
sleep $delay
proc=$(ps aux | grep "$cmd" | grep -v grep | grep -v bash)
echo "$proc"
pids=$(echo "$proc" | awk '{print $2}')
flag=0
########## wait until all pids exit ##############
## edit from https://unix.stackexchange.com/questions/305039/pausing-a-bash-script-until-previous-commands-are-finished
while [ $flag -eq 0 ]; do
    for PID in $(echo ${pids[@]}); do
        flag=1
        ps -ef | grep ${PID} | grep -v grep >/dev/null
        r=${?}
        if [ ${r} -eq 0 ]; then
            flag=0
        fi
    done
done
########################
end=$(date +%s.%N)
runtime=$(echo "$end - $start " | bc -l)
echo "time cost = $runtime sec. "

實際運作結果如下,非平行處理在終端機執行:

Python入門./exe.sh "python example_no_multi.py"

得到結果:

execute command = python example_no_multi.py
running process:
whuang0+   10141  2.0  0.0  27676  9652 pts/1    S+   11:27   0:00 python example_no_multi.py
final result:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]
time cost = 32.056646352 sec. 

也就是每筆資料使用2秒,總共消耗2×16=32秒,並且只有一個主進程負責

平行處理在終端機執行:

 ./exe.sh "python example.py"

得到結果:

execute command = python example.py
running process:
whuang0+   20767  4.0  0.0 251352 12312 pts/3    Sl+  11:28   0:00 python example.py
whuang0+   20768  0.0  0.0  30156  9844 pts/3    S+   11:28   0:00 python example.py
whuang0+   20769  0.0  0.0  30156  9848 pts/3    S+   11:28   0:00 python example.py
whuang0+   20770  0.0  0.0  30156  9852 pts/3    S+   11:28   0:00 python example.py
whuang0+   20771  0.0  0.0  30156  9852 pts/3    S+   11:28   0:00 python example.py
whuang0+   20772  0.0  0.0  30156  9856 pts/3    S+   11:28   0:00 python example.py
whuang0+   20773  0.0  0.0  30156  9860 pts/3    S+   11:28   0:00 python example.py
whuang0+   20774  0.0  0.0  30156  9864 pts/3    S+   11:28   0:00 python example.py
whuang0+   20775  0.0  0.0  30156  9864 pts/3    S+   11:28   0:00 python example.py
whuang0+   20776  0.0  0.0  30156  9864 pts/3    S+   11:28   0:00 python example.py
whuang0+   20777  0.0  0.0  30156  9864 pts/3    S+   11:28   0:00 python example.py
whuang0+   20778  0.0  0.0  30156  9868 pts/3    S+   11:28   0:00 python example.py
whuang0+   20779  0.0  0.0  30156  9864 pts/3    S+   11:28   0:00 python example.py
whuang0+   20780  0.0  0.0  30156  9864 pts/3    S+   11:28   0:00 python example.py
whuang0+   20781  0.0  0.0  30156  9868 pts/3    S+   11:28   0:00 python example.py
whuang0+   20782  0.0  0.0  30156  9868 pts/3    S+   11:28   0:00 python example.py
whuang0+   20783  0.0  0.0  30156 10004 pts/3    S+   11:28   0:00 python example.py
final result:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]
time cost = 2.211762556 sec. 

可以看到主進程為1個,另外有16個子進程,並且總耗費時間只有2秒左右。如此一來我們便成功實驗了使用Pool.map的方式加速處理資料。

✔ 幫我們按個喜歡和分享,支持我們

平均分數 0 / 5. 給分人數 0

尚未有評分,請幫我評分歐

楊 明翰
楊 明翰

是一名八年級中段班的創業者與資料科學家

“With belief and action, we change the world.”

憑藉信念與行動,我們改變世界💪

更多關於站長

本文允許重製、散布、傳輸以及修改,但不得為商業目的之使用

使用時必須註明出處自:楊明翰 , 台灣人工智慧與資料科學研究室 https://aistudio.tw

標籤: 多進程 平行處理

文章導覽

❮ Previous Post: 【個人演講】精通機器學習之路從手刻數學開始
Next Post: 【 資料工程與訊號分析】Hampel 濾波器以及中位數絕對離差MAD檢測離群值 ❯

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

更多AI相關資訊,歡迎加入站長的粉絲團

Facebook

閱讀視覺風格選擇


實驗袍白 / 適合資料分析師、科學家
終端機黑 / 適合黑客、電腦工程師

專欄列表

  • Artificial intelligence 人工智慧 (3)
    • HPC 高效能運算 (1)
    • PyTorch (1)
    • 從頭實作AI (1)
  • Backend 後端開發 (1)
  • C++ 應用 (2)
  • Community 社群介紹 (1)
  • Data Engineering 資料工程與訊號分析 (1)
  • Java 開發與教學 (2)
  • Linux 入門 (5)
  • NLP/NLU 自然語言處理與理解 (4)
  • Philosophy 站長哲學 (5)
  • Python 開發與教學 (7)
    • Python Flask 網站開發 (1)
    • Python 中階 (1)
    • Python 入門 (5)
  • Slides會議簡報 (12)
  • Start-up 創新創業 (7)
  • 機器學習數學基礎 (1)
  • 私人筆記手稿 (4)
  • 線上教學目錄 (4)

近期發布

  • 【Linux 應用】使用Zenity零成本開發的多開瀏覽器管理器
  • 【Start-up 創新創業】如何選擇創業題目
  • 【自然語言處理與理解】Llama-2大語言模型CPU版本使用
  • 【個人演講】AI EXPO 2023 ChatGPT以及LLM
  • 【自訓課程】NGS 次世代基因體資料科學 課程

討論

尚無留言可供顯示。

年份彙整

  • 2023 (9)
  • 2022 (4)
  • 2021 (6)
  • 2020 (14)
  • 2019 (8)
  • 2018 (10)
  • 2017 (2)
  • 2016 (4)
  • 2015 (2)

ChatGPT GRU HPC llama-cpp LLM Log4J LSTM Node.js SMO SVM Zenity 人工智慧 人格特質 出資 創新 創業 多語言 多開瀏覽器 大語言模型 感知機 戰鬥 技術 技術合夥 技術股 撰寫程式 新創 新手 新聞輿情 最佳化 機器學習 死亡 現代C++ 系統日誌 股東權益 能力 證明 變數 負債 資本 資產負債表 長短期記憶 霍普菲爾 類神經網 類神經網路 風險

調整字型大小

A 縮小字型大小。 A 重設字型大小。 A 放大字型大小。

Copyright © 2023 AI STUDIO Taiwan.
聯絡我們 : whuang022@gmail.com
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Theme: Oceanly by ScriptsTown