Python之并行--基于joblib
Python之并行–基于joblib
Python的并行遠不如Matlab好用。比如Matlab里面并行就直接把for改成parfor就行(當然還要注意迭代時下標的格式),而Python查 一查并行,各種亂七八糟的方法一大堆,而且最不爽的一點就是只能對函數進行并行。
當然,這點困難也肯定不能就難倒我們,該克服也得克服,畢竟從本質上講,也就只是實現的方式換一換而已。
大名鼎鼎的sklearn里面集成了很方便的并行計算,這在之前的機器學習教程里面也有12
仔細查看其代碼發現就是用joblib實現的,并且用法還挺巧。
from joblib import Parallel, delayedparallel = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,pre_dispatch=self.pre_dispatch) out = parallel(delayed(_fit_and_score)(clone(base_estimator),X, y,train=train, test=test,parameters=parameters,**fit_and_score_kwargs)for parameters, (train, test)in product(candidate_params,cv.split(X, y, groups)))這段代碼的意思非常簡單,即是用n_jobs個CPU來計算_fit_and_score函數,其中參數為clone(base_estimator), X, y,train=train, test=test,parameters=parameters,**fit_and_score_kwargs,而這里只有parameters,(train,test)作為被枚舉的變量,其它參數始終保持不變。至于里為何要用clone函數是因為如果直接將base_estimator傳入的話,這個模型在外部也將會被改變。具體原因可以參看其它文檔。
這里就簡單回顧下joblib的用法:
使用之前可以在自己的環境里先安裝好這個庫:
pip install joblib1、簡單示例
首先joblib里面最常用到的一個類和一個方法分別是Parallel和delayed。Parallel主要用于初始化并行計算時需要用到的參數,而delayed則主要用來指定需要被并行的參數。比如官方給出的以下示例:
from math import sqrt from joblib import Parallel, delayed Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10)) [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]這段代碼其實已經基本看出方法的主要使用模式了。
解釋一下:
- Parallel(n_jobs=2): 指定兩個CPU(默認是分配給不同的CPU)
- 后面的delayed(sqrt)表示要用的函數是sqrt,這里這種用法就非常類似C++里面的委托(delegate)。
- (i ** 2) for i in range(10): 這里注意(i**2)的括號和delayed(sqrt)是緊挨著的。這一小段表示要傳遞給delayed中指定的函數的參數是i^2。
那么結合這么一小段程序,其實已經能大致理解它的使用方法了。這里最開始可能主要不習慣的是要用到了Python里面的內部函數機制。
當然,作為 調包俠 工程師的我們,先不管這么多,用起來再說。比如寫點簡單的hello world
c = 'hello world' Parallel(n_jobs=2)(delayed(print)(i) for i in c)Out: [None, None, None, None, None, None, None, None, None, None, None]這里就出現問題了,因為直接用print函數并沒有返回值。所以這種實現方式是不正確的。要解決這個問題可以用另外一種方式來實現,比如:
def test_print(c):for i in c:print(i)return cParallel(n_jobs=2)(delayed(test_print)(i) for i in test_print(c)) h e l l ow o r l d Out[8]: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']這里注意,真正實現了print功能的是后面的函數test_print(c)。
2、多參數并行
由于Python提供了很好的多參數同時并行機制,可以用product、zip等方法將多個集合放在一起,那么就可以實現多參數的并行。比如:
import numpy as np def test_mvar(a,b):return a + b Parallel(n_jobs=5)(delayed(test_mvar)(i,j) for i,j in zip(np.random.rand(10),np.random.rand(10)))[0.8787518714894541,1.3086203058912202,1.3478235487198926,1.1963547010071447,1.2705711575828578,0.5354314753331146,1.0794490655604165,0.6707125925684075,0.8861808920187632,1.1512627654053902]當然注意到開頭的代碼,函數中可以只并行一部分參數,這里我們也可以如此,比如:
Parallel(n_jobs=5)(delayed(test_mvar)(i,1) for i,j in zip(np.random.rand(10),np.random.rand(10)))[1.2137475752125968,1.4543891439818082,1.952684349026534,1.3565884407873496,1.5411521108342199,1.0820532116263255,1.8668685545516257,1.397012511283467,1.1645324713909715,1.899119157699394]下面這段代碼就只計算數字1和一系列隨機數的和。
那么到這里可以簡單再分析一下用法:delayed函數指定了被運行的主函數test_mvar,而其后的參數則是由元組形式給出(i,1),后面需要進行for循環的參數只要保持和前面元組中相同的變量名即可。另外,需要注意的是變量的順序必須和被運行的主函數保持一致。
3、并行時CPU是怎么分配的
這個問題相對比較麻煩。在joblib中默認是采用loky實現并行,這種方式最為簡單直接,它會自然地將任務分配到多個CPU上去運行,同時更加穩定。當然除此之外還有其它的一些方式,比如多進程。這些方法之間的區別在我們大多數時候是相對比較細微的(追求極致并行的除外),我們只需要對它有個簡單直接的了解即可。
再看一個例子:
from joblib import Parallel, delayedimport numpy as npdef test_non(a):return ac = range(20)Parallel(n_jobs=2,backend='multiprocessing')(delayed(test_non)(i) for i in c)注意,這里什么也沒有發生。我們把并行方式改成了多進程,出現的結果就是一直卡著。這里主要就是分配機制的問題。所以一般而言,直接采用默認方式就好。
4、何時選用并行
這個問題其實是最關鍵的。并行其實也不一定一直會很快。因為并行的處理流程實際上是這樣的:
#mermaid-svg-ohbAB6uBtD5r0EVV .label {font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family);fill: #333;color: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .label text {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .node rect, #mermaid-svg-ohbAB6uBtD5r0EVV .node circle, #mermaid-svg-ohbAB6uBtD5r0EVV .node ellipse, #mermaid-svg-ohbAB6uBtD5r0EVV .node polygon, #mermaid-svg-ohbAB6uBtD5r0EVV .node path {fill: #ECECFF;stroke: #9370DB;stroke-width: 1px; }#mermaid-svg-ohbAB6uBtD5r0EVV .node .label {text-align: center;fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .node.clickable {cursor: pointer; }#mermaid-svg-ohbAB6uBtD5r0EVV .arrowheadPath {fill: #333333; }#mermaid-svg-ohbAB6uBtD5r0EVV .edgePath .path {stroke: #333333;stroke-width: 1.5px; }#mermaid-svg-ohbAB6uBtD5r0EVV .flowchart-link {stroke: #333333;fill: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .edgeLabel {background-color: #e8e8e8;text-align: center; } #mermaid-svg-ohbAB6uBtD5r0EVV .edgeLabel rect {opacity: 0.9; } #mermaid-svg-ohbAB6uBtD5r0EVV .edgeLabel span {color: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .cluster rect {fill: #ffffde;stroke: #aaaa33;stroke-width: 1px; }#mermaid-svg-ohbAB6uBtD5r0EVV .cluster text {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV div.mermaidTooltip {position: absolute;text-align: center;max-width: 200px;padding: 2px;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family);font-size: 12px;background: #ffffde;border: 1px solid #aaaa33;border-radius: 2px;pointer-events: none;z-index: 100; }#mermaid-svg-ohbAB6uBtD5r0EVV .actor {stroke: #CCCCFF;fill: #ECECFF; }#mermaid-svg-ohbAB6uBtD5r0EVV text.actor > tspan {fill: black;stroke: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .actor-line {stroke: grey; }#mermaid-svg-ohbAB6uBtD5r0EVV .messageLine0 {stroke-width: 1.5;stroke-dasharray: none;stroke: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .messageLine1 {stroke-width: 1.5;stroke-dasharray: 2, 2;stroke: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV #arrowhead path {fill: #333;stroke: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .sequenceNumber {fill: white; }#mermaid-svg-ohbAB6uBtD5r0EVV #sequencenumber {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV #crosshead path {fill: #333;stroke: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .messageText {fill: #333;stroke: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .labelBox {stroke: #CCCCFF;fill: #ECECFF; }#mermaid-svg-ohbAB6uBtD5r0EVV .labelText,#mermaid-svg-ohbAB6uBtD5r0EVV .labelText > tspan {fill: black;stroke: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .loopText,#mermaid-svg-ohbAB6uBtD5r0EVV .loopText > tspan {fill: black;stroke: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .loopLine {stroke-width: 2px;stroke-dasharray: 2, 2;stroke: #CCCCFF;fill: #CCCCFF; }#mermaid-svg-ohbAB6uBtD5r0EVV .note {stroke: #aaaa33;fill: #fff5ad; }#mermaid-svg-ohbAB6uBtD5r0EVV .noteText,#mermaid-svg-ohbAB6uBtD5r0EVV .noteText > tspan {fill: black;stroke: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .activation0 {fill: #f4f4f4;stroke: #666; }#mermaid-svg-ohbAB6uBtD5r0EVV .activation1 {fill: #f4f4f4;stroke: #666; }#mermaid-svg-ohbAB6uBtD5r0EVV .activation2 {fill: #f4f4f4;stroke: #666; }#mermaid-svg-ohbAB6uBtD5r0EVV .mermaid-main-font {font-family: "trebuchet ms", verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .section {stroke: none;opacity: 0.2; }#mermaid-svg-ohbAB6uBtD5r0EVV .section0 {fill: rgba(102, 102, 255, 0.49); }#mermaid-svg-ohbAB6uBtD5r0EVV .section2 {fill: #fff400; }#mermaid-svg-ohbAB6uBtD5r0EVV .section1, #mermaid-svg-ohbAB6uBtD5r0EVV .section3 {fill: white;opacity: 0.2; }#mermaid-svg-ohbAB6uBtD5r0EVV .sectionTitle0 {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .sectionTitle1 {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .sectionTitle2 {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .sectionTitle3 {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .sectionTitle {text-anchor: start;font-size: 11px;text-height: 14px;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .grid .tick {stroke: lightgrey;opacity: 0.8;shape-rendering: crispEdges; } #mermaid-svg-ohbAB6uBtD5r0EVV .grid .tick text {font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .grid path {stroke-width: 0; }#mermaid-svg-ohbAB6uBtD5r0EVV .today {fill: none;stroke: red;stroke-width: 2px; }#mermaid-svg-ohbAB6uBtD5r0EVV .task {stroke-width: 2; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskText {text-anchor: middle;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .taskText:not([font-size]) {font-size: 11px; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutsideRight {fill: black;text-anchor: start;font-size: 11px;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutsideLeft {fill: black;text-anchor: end;font-size: 11px; }#mermaid-svg-ohbAB6uBtD5r0EVV .task.clickable {cursor: pointer; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskText.clickable {cursor: pointer;fill: #003163 !important;font-weight: bold; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutsideLeft.clickable {cursor: pointer;fill: #003163 !important;font-weight: bold; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutsideRight.clickable {cursor: pointer;fill: #003163 !important;font-weight: bold; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskText0, #mermaid-svg-ohbAB6uBtD5r0EVV .taskText1, #mermaid-svg-ohbAB6uBtD5r0EVV .taskText2, #mermaid-svg-ohbAB6uBtD5r0EVV .taskText3 {fill: white; }#mermaid-svg-ohbAB6uBtD5r0EVV .task0, #mermaid-svg-ohbAB6uBtD5r0EVV .task1, #mermaid-svg-ohbAB6uBtD5r0EVV .task2, #mermaid-svg-ohbAB6uBtD5r0EVV .task3 {fill: #8a90dd;stroke: #534fbc; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutside0, #mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutside2 {fill: black; }#mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutside1, #mermaid-svg-ohbAB6uBtD5r0EVV .taskTextOutside3 {fill: black; }#mermaid-svg-ohbAB6uBtD5r0EVV .active0, #mermaid-svg-ohbAB6uBtD5r0EVV .active1, #mermaid-svg-ohbAB6uBtD5r0EVV .active2, #mermaid-svg-ohbAB6uBtD5r0EVV .active3 {fill: #bfc7ff;stroke: #534fbc; }#mermaid-svg-ohbAB6uBtD5r0EVV .activeText0, #mermaid-svg-ohbAB6uBtD5r0EVV .activeText1, #mermaid-svg-ohbAB6uBtD5r0EVV .activeText2, #mermaid-svg-ohbAB6uBtD5r0EVV .activeText3 {fill: black !important; }#mermaid-svg-ohbAB6uBtD5r0EVV .done0, #mermaid-svg-ohbAB6uBtD5r0EVV .done1, #mermaid-svg-ohbAB6uBtD5r0EVV .done2, #mermaid-svg-ohbAB6uBtD5r0EVV .done3 {stroke: grey;fill: lightgrey;stroke-width: 2; }#mermaid-svg-ohbAB6uBtD5r0EVV .doneText0, #mermaid-svg-ohbAB6uBtD5r0EVV .doneText1, #mermaid-svg-ohbAB6uBtD5r0EVV .doneText2, #mermaid-svg-ohbAB6uBtD5r0EVV .doneText3 {fill: black !important; }#mermaid-svg-ohbAB6uBtD5r0EVV .crit0, #mermaid-svg-ohbAB6uBtD5r0EVV .crit1, #mermaid-svg-ohbAB6uBtD5r0EVV .crit2, #mermaid-svg-ohbAB6uBtD5r0EVV .crit3 {stroke: #ff8888;fill: red;stroke-width: 2; }#mermaid-svg-ohbAB6uBtD5r0EVV .activeCrit0, #mermaid-svg-ohbAB6uBtD5r0EVV .activeCrit1, #mermaid-svg-ohbAB6uBtD5r0EVV .activeCrit2, #mermaid-svg-ohbAB6uBtD5r0EVV .activeCrit3 {stroke: #ff8888;fill: #bfc7ff;stroke-width: 2; }#mermaid-svg-ohbAB6uBtD5r0EVV .doneCrit0, #mermaid-svg-ohbAB6uBtD5r0EVV .doneCrit1, #mermaid-svg-ohbAB6uBtD5r0EVV .doneCrit2, #mermaid-svg-ohbAB6uBtD5r0EVV .doneCrit3 {stroke: #ff8888;fill: lightgrey;stroke-width: 2;cursor: pointer;shape-rendering: crispEdges; }#mermaid-svg-ohbAB6uBtD5r0EVV .milestone {transform: rotate(45deg) scale(0.8, 0.8); }#mermaid-svg-ohbAB6uBtD5r0EVV .milestoneText {font-style: italic; }#mermaid-svg-ohbAB6uBtD5r0EVV .doneCritText0, #mermaid-svg-ohbAB6uBtD5r0EVV .doneCritText1, #mermaid-svg-ohbAB6uBtD5r0EVV .doneCritText2, #mermaid-svg-ohbAB6uBtD5r0EVV .doneCritText3 {fill: black !important; }#mermaid-svg-ohbAB6uBtD5r0EVV .activeCritText0, #mermaid-svg-ohbAB6uBtD5r0EVV .activeCritText1, #mermaid-svg-ohbAB6uBtD5r0EVV .activeCritText2, #mermaid-svg-ohbAB6uBtD5r0EVV .activeCritText3 {fill: black !important; }#mermaid-svg-ohbAB6uBtD5r0EVV .titleText {text-anchor: middle;font-size: 18px;fill: black;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV g.classGroup text {fill: #9370DB;stroke: none;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family);font-size: 10px; } #mermaid-svg-ohbAB6uBtD5r0EVV g.classGroup text .title {font-weight: bolder; }#mermaid-svg-ohbAB6uBtD5r0EVV g.clickable {cursor: pointer; }#mermaid-svg-ohbAB6uBtD5r0EVV g.classGroup rect {fill: #ECECFF;stroke: #9370DB; }#mermaid-svg-ohbAB6uBtD5r0EVV g.classGroup line {stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV .classLabel .box {stroke: none;stroke-width: 0;fill: #ECECFF;opacity: 0.5; }#mermaid-svg-ohbAB6uBtD5r0EVV .classLabel .label {fill: #9370DB;font-size: 10px; }#mermaid-svg-ohbAB6uBtD5r0EVV .relation {stroke: #9370DB;stroke-width: 1;fill: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .dashed-line {stroke-dasharray: 3; }#mermaid-svg-ohbAB6uBtD5r0EVV #compositionStart {fill: #9370DB;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #compositionEnd {fill: #9370DB;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #aggregationStart {fill: #ECECFF;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #aggregationEnd {fill: #ECECFF;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #dependencyStart {fill: #9370DB;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #dependencyEnd {fill: #9370DB;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #extensionStart {fill: #9370DB;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV #extensionEnd {fill: #9370DB;stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV .commit-id, #mermaid-svg-ohbAB6uBtD5r0EVV .commit-msg, #mermaid-svg-ohbAB6uBtD5r0EVV .branch-label {fill: lightgrey;color: lightgrey;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .pieTitleText {text-anchor: middle;font-size: 25px;fill: black;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .slice {font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV g.stateGroup text {fill: #9370DB;stroke: none;font-size: 10px;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV g.stateGroup text {fill: #9370DB;fill: #333;stroke: none;font-size: 10px; }#mermaid-svg-ohbAB6uBtD5r0EVV g.statediagram-cluster .cluster-label text {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV g.stateGroup .state-title {font-weight: bolder;fill: black; }#mermaid-svg-ohbAB6uBtD5r0EVV g.stateGroup rect {fill: #ECECFF;stroke: #9370DB; }#mermaid-svg-ohbAB6uBtD5r0EVV g.stateGroup line {stroke: #9370DB;stroke-width: 1; }#mermaid-svg-ohbAB6uBtD5r0EVV .transition {stroke: #9370DB;stroke-width: 1;fill: none; }#mermaid-svg-ohbAB6uBtD5r0EVV .stateGroup .composit {fill: white;border-bottom: 1px; }#mermaid-svg-ohbAB6uBtD5r0EVV .stateGroup .alt-composit {fill: #e0e0e0;border-bottom: 1px; }#mermaid-svg-ohbAB6uBtD5r0EVV .state-note {stroke: #aaaa33;fill: #fff5ad; } #mermaid-svg-ohbAB6uBtD5r0EVV .state-note text {fill: black;stroke: none;font-size: 10px; }#mermaid-svg-ohbAB6uBtD5r0EVV .stateLabel .box {stroke: none;stroke-width: 0;fill: #ECECFF;opacity: 0.7; }#mermaid-svg-ohbAB6uBtD5r0EVV .edgeLabel text {fill: #333; }#mermaid-svg-ohbAB6uBtD5r0EVV .stateLabel text {fill: black;font-size: 10px;font-weight: bold;font-family: 'trebuchet ms', verdana, arial;font-family: var(--mermaid-font-family); }#mermaid-svg-ohbAB6uBtD5r0EVV .node circle.state-start {fill: black;stroke: black; }#mermaid-svg-ohbAB6uBtD5r0EVV .node circle.state-end {fill: black;stroke: white;stroke-width: 1.5; }#mermaid-svg-ohbAB6uBtD5r0EVV #statediagram-barbEnd {fill: #9370DB; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-cluster rect {fill: #ECECFF;stroke: #9370DB;stroke-width: 1px; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-cluster rect.outer {rx: 5px;ry: 5px; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-state .divider {stroke: #9370DB; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-state .title-state {rx: 5px;ry: 5px; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-cluster.statediagram-cluster .inner {fill: white; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-cluster.statediagram-cluster-alt .inner {fill: #e0e0e0; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-cluster .inner {rx: 0;ry: 0; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-state rect.basic {rx: 5px;ry: 5px; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-state rect.divider {stroke-dasharray: 10,10;fill: #efefef; }#mermaid-svg-ohbAB6uBtD5r0EVV .note-edge {stroke-dasharray: 5; }#mermaid-svg-ohbAB6uBtD5r0EVV .statediagram-note rect {fill: #fff5ad;stroke: #aaaa33;stroke-width: 1px;rx: 0;ry: 0; }:root {--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive; }#mermaid-svg-ohbAB6uBtD5r0EVV .error-icon {fill: #552222; }#mermaid-svg-ohbAB6uBtD5r0EVV .error-text {fill: #552222;stroke: #552222; }#mermaid-svg-ohbAB6uBtD5r0EVV .edge-thickness-normal {stroke-width: 2px; }#mermaid-svg-ohbAB6uBtD5r0EVV .edge-thickness-thick {stroke-width: 3.5px; }#mermaid-svg-ohbAB6uBtD5r0EVV .edge-pattern-solid {stroke-dasharray: 0; }#mermaid-svg-ohbAB6uBtD5r0EVV .edge-pattern-dashed {stroke-dasharray: 3; }#mermaid-svg-ohbAB6uBtD5r0EVV .edge-pattern-dotted {stroke-dasharray: 2; }#mermaid-svg-ohbAB6uBtD5r0EVV .marker {fill: #333333; }#mermaid-svg-ohbAB6uBtD5r0EVV .marker.cross {stroke: #333333; }:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-ohbAB6uBtD5r0EVV {color: rgba(0, 0, 0, 0.75);font: ;}原任務拆分任務各CPU分別完成任務合并任務,返回結果這里拆分、合并和CPU之間的通信都是會產生時間消耗的。那么很容易想到,如果本身的任務很小,其實消耗的時間反而會更多。另外,往往實際的并行不能達到幾個CPU就有幾倍速度也正是這個原因。比如:
- 直接循環
- 雙核
- 四核
可以很清楚地看到,這時核心數越多,反而時間越慢。
因此在使用并行時,也要嚴格根據自己的需求來。
sklearn快速入門教程:(四)模型自動調參 ??
sklearn快速入門教程:(五)集成學習 ??
總結
以上是生活随笔為你收集整理的Python之并行--基于joblib的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模糊数学笔记大全(模糊集、截集、模糊矩阵
- 下一篇: Python主要智能优化算法库汇总