マルチプロセッシング モジュールは、Python ライブラリの中で最も高度で強力なモジュールの 1 つです。この記事では、マルチプロセッシングの一般的なテクニックを簡単に紹介します
プロセスはシステム自体によって管理されます。
1: 最も基本的な書き方
from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': p = Pool(5) print(p.map(f, [1, 2, 3])) [1, 4, 9]
2. 実際、プロセスは os.fork メソッドを通じて生成されます
Unix では、すべてのプロセスは fork メソッドを通じて生成されます。
multiprocessing Process os info(title): title , __name__ (os, ): , os.getppid() , os.getpid() f(name): info() , name __name__ == : info() p = Process(=f, =(,)) p.start() p.join()
3. スレッド共有メモリ
threading run(info_list,n): info_list.append(n) info_list __name__ == : info=[] i (): p=threading.Thread(=run,=[info,i]) p.start() [0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
プロセスはメモリを共有しません:
multiprocessing Process run(info_list,n): info_list.append(n) info_list __name__ == : info=[] i (): p=Process(=run,=[info,i]) p.start() [1] [2] [3] [0] [4] [5] [6] [7] [8] [9]
メモリを共有したい場合は、Queue
rrを使用する必要があります。にいますmultiprocessing module ee4. ロック: 画面共有のみ、プロセスが独立しているため、複数のプロセスには役に立たない
multiprocessing Process, Queue f(q,n): q.put([n,]) __name__ == : q=Queue() i (): p=Process(=f,=(q,i)) p.start() : q.get()
5. プロセス間メモリ共有: 値、配列
multiprocessing Process, Lock f(l, i): l.acquire() , i l.release() __name__ == : lock = Lock() num (): Process(=f, =(lock, num)).start() hello world 0 hello world 1 hello world 2 hello world 3 hello world 4 hello world 5 hello world 6 hello world 7 hello world 8 hello world 9
#マネージャー共有方法ですが遅いです
multiprocessing Process, Value, Array f(n, a): n.value = i ((a)): a[i] = -a[i] __name__ == : num = Value(, ) arr = Array(, ()) num.value arr[:] p = Process(=f, =(num, arr)) p.start() p.join() 0.0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 3.1415927 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
#非同期: この書き込み方法はあまり使用されません
multiprocessing Process, Manager f(d, l): d[] = d[] = d[] = l.reverse() __name__ == : manager = Manager() d = manager.dict() l = manager.list(()) p = Process(=f, =(d, l)) p.start() p.join() d l # print '-------------'这里只是另一种写法 # print pool.map(f,range(10)) {0.25: None, 1: '1', '2': 2} [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
同期が適用されます
マルチプロセスについての単純な話に関連するその他の記事は、 Python の場合は、PHP 中国語 Web サイトに注意してください。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ホットトピック









