Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PythonParrotと学ぶPythonの並列実行
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
rf-castle
July 10, 2021
72
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PythonParrotと学ぶPythonの並列実行
rf-castle
July 10, 2021
More Decks by rf-castle
See All by rf-castle
Rustで競技プログラミング
rfcastle
0
60
Alter_Linuxのすすめ
rfcastle
0
170
Python_Parrotと学ぶPythonの並行実行.pdf
rfcastle
0
120
Nuxt jsに触れてみた
rfcastle
0
35
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
560
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
480
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
850
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
530
Ethics towards AI in product and experience design
skipperchong
2
380
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
What's in a price? How to price your products and services
michaelherold
247
13k
Exploring anti-patterns in Rails
aemeredith
4
510
Product Roadmaps are Hard
iamctodd
55
13k
Transcript
Python Parrotと学ぶ Pythonの並行実行 田城幸太
自己紹介 会津大学 学部三年 田城幸太 Zliの部長、通りすがりのPython Parrot
嫁:羽黒(艦これ) 妹:チノ(ごちうさ) 相棒:キィラン(ゼノンザード) Twitter: @kouta_tashiro
今回の内容 Pythonで使える並列実行ライブラリを紹介 threading(スレッドベースの並列実行) multiprocessing(プロセスベースの並列実行)
並列実行って? その名の通り、複数のコードを同時実行すること。 逐次処理により、1つずつ処理が実行されるが、 それでは1度に1つの処理しか行えない。 コアも1つしか使えない! 使用場面
マルチコアCPUのリソースを有効に使う。(multiprocessing) I/Oバウンドの間に別な処理を実行する。(threading)
None
None
None
None
使い方 threading.Threadか、multiprocessing.Processクラスを使う。 コンストラクタ引数(すべてキーワード付き引数) group:常にNoneにする。将来 ThreadGroup クラスが実装されたときの拡張用に予約され ている。
target: やらせたい処理をcallableオブジェクト(関数など)で渡す args,kwargs:↑に渡したい引数を指定する。 args:キーワードなし引数のタプル kwargs:キーワード付き引数の辞書型 daemon:デーモンスレッドであるかを指定する。 デーモンスレッド以外のスレッドがすべて終了すると、Pythonプログラム全体が終了する。 Noneにすると、現在のスレッドとおなじになる。
使い方2 メソッド start() スレッド・プロセスの活動を開始するメソッド。 join()
スレッド・プロセスが終わるまで待機するメソッド。 自分自身のスレッド・プロセスに対してこのメソッドを呼ぶとデッドロックを起こすため エラーになる。 (もしこれが許されれば永遠に待つことになる) まだ開始していない場合も同様。
CPUバウンドの場合 逐次実行 4.848s threading 5.039s
multiprocessing 1.521s
IOバウンドの場合 逐次実行 1.454s threading 0.558s
multiprocessing 0.574s
スレッドとプロセス
GIL(グローバルインタプリタロック) 複数スレッドがあっても、1度に1つのスレッドしかバイトコードを実行でき ない仕組み。 オブジェクトモデルが同時アクセスに対して暗黙的に安全になる。 おおざっぱに言うなら、threadingモジュールだと1つのコアしか使えない ただし、標準あるいは外部のいくつかの拡張モジュールは、圧縮やハッシュ計
算などの計算の重い処理をするときに GIL を解除するように設計されている。 Zlibや、hashlibなどがこれに該当。 また、I/O 処理をする場合 GIL は常に解除される。(別スレッドに処理を移せ る)
おわりに Pythonに限らず、並列実行は難しい。 Pythonで並列実行とかってあまり使わないイメージ? asyncioは比較的使うかも。 Pythonで並列実行やるときの皆様の参考になれば良いです。