導入
Python のマルチプロセッシング モジュールを使用すると、プロセスを作成および管理できるため、マシン上の複数のプロセッサを最大限に活用できます。スレッドが同じメモリ空間を共有するスレッドとは異なり、プロセスごとに個別のメモリ空間を使用することで並列実行を実現できます。以下は、マルチプロセッシング モジュールで一般的に使用されるクラスとメソッドのリストと簡単な例です。
1. プロセス
Process クラスはマルチプロセッシング モジュールのコアであり、新しいプロセスを作成して実行できます。
from multiprocessing import Process def print_numbers(): for i in range(5): print(i) p = Process(target=print_numbers) p.start() # Starts a new process p.join() # Waits for the process to finish
2.start()
プロセスのアクティビティを開始します。
p = Process(target=print_numbers) p.start() # Runs the target function in a separate process
3. join([タイムアウト])
join() メソッドが呼び出されるプロセスが終了するまで、呼び出しプロセスをブロックします。オプションで、タイムアウトを指定できます。
p = Process(target=print_numbers) p.start() p.join(2) # Waits up to 2 seconds for the process to finish
4. is_alive()
プロセスがまだ実行中の場合は True を返します。
p = Process(target=print_numbers) p.start() print(p.is_alive()) # True if the process is still running
5. current_process()
呼び出しプロセスを表す現在の Process オブジェクトを返します。
from multiprocessing import current_process def print_current_process(): print(current_process()) p = Process(target=print_current_process) p.start() # Prints the current process info
6. active_children()
現在有効なすべてのプロセス オブジェクトのリストを返します。
p1 = Process(target=print_numbers) p2 = Process(target=print_numbers) p1.start() p2.start() print(Process.active_children()) # Lists all active child processes
7.cpu_count()
マシンで使用可能な CPU の数を返します。
from multiprocessing import cpu_count print(cpu_count()) # Returns the number of CPUs on the machine
8. プール
Pool オブジェクトは、複数の入力値にわたる関数の実行を並列化する便利な方法を提供します。ワーカープロセスのプールを管理します。
from multiprocessing import Pool def square(n): return n * n with Pool(4) as pool: # Pool with 4 worker processes result = pool.map(square, [1, 2, 3, 4, 5]) print(result) # [1, 4, 9, 16, 25]
9. 待ち行列
キューは、複数のプロセス間でデータを受け渡して通信できるようにする共有データ構造です。
from multiprocessing import Process, Queue def put_data(q): q.put([1, 2, 3]) def get_data(q): data = q.get() print(data) q = Queue() p1 = Process(target=put_data, args=(q,)) p2 = Process(target=get_data, args=(q,)) p1.start() p2.start() p1.join() p2.join()
10.ロック
ロックにより、一度に 1 つのプロセスだけが共有リソースにアクセスできるようになります。
from multiprocessing import Process, Lock lock = Lock() def print_numbers(): with lock: for i in range(5): print(i) p1 = Process(target=print_numbers) p2 = Process(target=print_numbers) p1.start() p2.start() p1.join() p2.join()
11. 値と配列
Value オブジェクトと Array オブジェクトを使用すると、プロセス間で単純なデータ型と配列を共有できます。
from multiprocessing import Process, Value def increment(val): with val.get_lock(): val.value += 1 shared_val = Value('i', 0) processes = [Process(target=increment, args=(shared_val,)) for _ in range(10)] for p in processes: p.start() for p in processes: p.join() print(shared_val.value) # Output will be 10
12.パイプ
パイプは、2 つのプロセス間の双方向通信チャネルを提供します。
from multiprocessing import Process, Pipe def send_message(conn): conn.send("Hello from child") conn.close() parent_conn, child_conn = Pipe() p = Process(target=send_message, args=(child_conn,)) p.start() print(parent_conn.recv()) # Receives data from the child process p.join()
13. マネージャー
マネージャーを使用すると、複数のプロセスが同時に変更できるリストや辞書などの共有オブジェクトを作成できます。
from multiprocessing import Process, Manager def modify_list(shared_list): shared_list.append("New item") with Manager() as manager: shared_list = manager.list([1, 2, 3]) p = Process(target=modify_list, args=(shared_list,)) p.start() p.join() print(shared_list) # [1, 2, 3, "New item"]
14. セマフォ
セマフォを使用すると、リソースへのアクセスを制御し、一度に特定の数のプロセスのみにアクセスを許可できます。
from multiprocessing import Process, Semaphore import time sem = Semaphore(2) # Only 2 processes can access the resource def limited_access(): with sem: print("Accessing resource") time.sleep(2) processes = [Process(target=limited_access) for _ in range(5)] for p in processes: p.start() for p in processes: p.join()
結論
Python のマルチプロセッシング モジュールは、マシン上の複数のプロセッサを最大限に活用するように設計されています。 Process を使用したプロセスの作成と管理から、Lock と Semaphore を使用した共有リソースの制御、Queue と Pipe を介した通信の促進に至るまで、マルチプロセッシング モジュールは、Python アプリケーションでタスクを並列化するために不可欠です。
以上が例を含む Python マルチプロセッシング モジュールのクイック ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

toAppendElementStoapyThonList、usetheappend()methodforsingleelements、extend()formultipleElements、andinsert()forspecificopsitions.1)useappend()foraddingoneElementatheend.2)useextend()toaddmultipleelementseffictience.3)

To CreateapythonList、usesquareBrackets []およびSeparateItemswithcommas.1)listsaredynamicandcanholdmixdatatypes.2)useappend()、remaid()、andslicingformanipulation.3)listcompreheNsionsionsionsionsionsionsionsionsionsionsionsionsionsionsionsionsionsientionforcreating.4)

金融、科学研究、医療、およびAIの分野では、数値データを効率的に保存および処理することが重要です。 1)財務では、メモリマッピングされたファイルとnumpyライブラリを使用すると、データ処理速度が大幅に向上する可能性があります。 2)科学研究の分野では、HDF5ファイルはデータストレージと取得用に最適化されています。 3)医療では、インデックス作成やパーティション化などのデータベース最適化テクノロジーがデータのパフォーマンスを向上させます。 4)AIでは、データシャーディングと分散トレーニングがモデルトレーニングを加速します。システムのパフォーマンスとスケーラビリティは、適切なツールとテクノロジーを選択し、ストレージと処理速度の間のトレードオフを検討することにより、大幅に改善できます。

pythonarraysarasarecreatedusingthearraymodule、notbuilt-inlikelists.1)importthearraymodule.2)specifytheTypecode、emg。、 'i'forintegers.3)Arraysofferbettermemoreefficiency forhomogeneousdatabutlasefutablethanlists。

Shebangラインに加えて、Pythonインタープリターを指定するには多くの方法があります。1。コマンドラインから直接Pythonコマンドを使用します。 2。バッチファイルまたはシェルスクリプトを使用します。 3. makeやcmakeなどのビルドツールを使用します。 4. Invokeなどのタスクランナーを使用します。各方法には利点と短所があり、プロジェクトのニーズに合った方法を選択することが重要です。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

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

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。
