Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
つまらないシステム(1) Scala の Future と ExecutionContext
Search
mashijp
November 13, 2015
Programming
2.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
つまらないシステム(1) Scala の Future と ExecutionContext
Scalaで詰まらないシステムを作るために覚えていてほしいこと。
mashijp
November 13, 2015
More Decks by mashijp
See All by mashijp
幅広い環境でSSL対応するために知っておくべきこと
mashijp
7
3.2k
SSL勉強会 - 「クラウドを支えるこれからの暗号技術」を読んで
mashijp
0
500
つまらないシステム(2) - Scala 書きやすすぎるFutureの罠
mashijp
0
77
Other Decks in Programming
See All in Programming
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
760
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
0
130
メールのエイリアス機能を履き違えない
isshinfunada
0
230
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
900
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
140
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
170
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
200
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
680
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
270
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
220
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
820
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
Featured
See All Featured
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
370
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
370
The Cost Of JavaScript in 2023
addyosmani
55
10k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
4 Signs Your Business is Dying
shpigford
187
22k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
WCS-LA-2024
lcolladotor
0
790
Leo the Paperboy
mayatellez
8
2.1k
The Curse of the Amulet
leimatthew05
2
14k
Transcript
ͭ·Βͳ͍γεςϜ(1) - FutureͱExecutionContext - mashi
ఆͯ͠Δಡऀ • ৽ଔͷΈͳ͞Μ • Scalaॻ͘ͷָ͕͘͠ͳ͖ͬͯͨ • Future͍͢͝ʂָ͍͠ʂ
Future[T] ͱ • ͍͔ͭT͕ฦͬͯ͘Δ • ͍ͭʁ – Θ͔Μͳ͍
Future ͷ࡞Γํ • Future.apply Λ͏ def apply(f: => T)(implicit ec:
ExecutionContext): T • f Ͱͨؔ͠ec: ExecutionContext ʹ Α࣮ͬͯߦ͞ΕΔ – ͜ͷ f Λࠓ͔Β Runnable ͱݺͼ·͢ • ͍࣮ͭߦ͞ΕΔͷ͔ – Θ͔Μͳ͍ (ExecutionContext ͕ܾΊΔ) ৄ͘͠http://mashi.hatenablog.com/entry/2014/11/24/010417 Scala ExecutionContextͬͯԿ/ FutureεϨου্ཱͪ͛͡Όͳ͍Α
ExecutionContext ͷҰൠతͳ࣮ Thread 1 Thread 2 Thread 3 Thread 4
• ༩͑ͨ Runnable Λۭ͍ͯΔεϨουͰ͍ ͍ײ͡ʹ࣮ߦ͢Δ Runnable 1 Runnable 2 Runnable 3 Runnable n ۭ͖࣍ୈ࣮ߦ͢Δ
ৗʹొ͢ΔExecutionContext • Future Λ͍͘͡Δʹৗʹ ExecutionContext ΛٻΊΒΕΔ • ීஈॻ͍͍ͯΔίʔυ val dbData:
Future[A] = getFromDB() val result: Future[B] = dbData.map({ e => convertToB(e) })
ৗʹొ͢ΔExecutionContext • ීஈԿؾͳ͘ॻ͍͍ͯΔίʔυ࣮ implicit ͰExecutionContextΛ͍ͯ͠Δ • mapʹͨؔ͠Λ࣮ߦ͢Δ ExecutionContext Λ҉తʹࢦఆ͍ͯ͠Δ val
dbData: Future[A] = getFromDB() val result: Future[B] = dbData.map({ e => convertToB(e) })(ec: ExecutionContext)
ExecutionContext ༻్ผʹఆٛ͢͠ • ྫ) memcached ͱ MySQL Ͱ͚Δ • ྫ)
MySQL ͷσʔλϕʔε1 ͱ 2 Ͱ͚Δ • ྫ) MySQL ͷ master ͱ slaveͰ͚Δ • ͳ͔ͥʁ
ExecutionContext ٧·Δ • ϒϩοΩϯά͢ΔͷΛ͏ͱ٧·Δ – ྫ͑ MySQL ͷૢ࡞٧·Δ ˞͜͜Ͱলུ͢Δ͕ɺscala-redis-nb ͷΑ͏ͳϊϯϒϩοΩϯάͳ࣮ Λ͏ͱجຊతʹ٧·Βͳ͍
1つのExecutionContextΛ͍·Θͨ͠߹ • ྫ͑MySQL ͱmemcached͕͍ͬͯΔγεςϜ • MySQLͷԠ͕͘ͳΔͱશ෦ॏ͘ͳΔ Thread 1 Thread 2
Thread 3 Thread 4 MySQLૢ࡞ MySQLૢ࡞ MySQLૢ࡞ 1ͭͷExecutionContext Λྲྀ༻ memcached ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ ͦͷଞͷ ૢ࡞ ؔͳ͍ૢ࡞·Ͱಉ͡Ωϡʔʹ٧·ΕΔͷͰ memcached ͷૢ࡞ॏ͘ͳΔ MySQL͔Β ͷԠͪ…
ExecutionContextΛ༻్ผʹ͚ͨ߹ • ͋ΔՕॴ͕٧·ͬͯଞʹӨڹ͠ͳ͘ͳΔ Thread 1 Thread 2 MySQLૢ࡞ MySQLૢ࡞ MySQLૢ࡞
memcached ૢ࡞ MySQL ૢ࡞ MySQL ૢ࡞ ͦͷଞͷ ૢ࡞ Thread 1 Thread 2 Thread 1 Thread 2 ͬͪ͜ʹӨڹ͠ͳ͍ MySQL͚͕ͩ٧·Δ
·ͱΊ • ීஈؾʹ͍ͯ͠ͳͯ͘implicitͰ҉తʹ ExecutionContextΛ͍ͯ͠Δ͜ͱ͕͋Δ – 'VUVSFͷNBQqBU.BQΛ͢Δͱඞͣͯ͠ ͍Δ • map flatMap
Λ͢Δͱ͖ʹࠓ͜͜Ͱ͍ͯ͠ ΔExecutionContextΛ٧·ΒͤΔڪΕ͕ͳ͍͔Λ ৗʹҙࣝ͢͠ – ࣮ɾϨϏϡʔ࣌ʹΑ͘ݟ·͠ΐ͏ • ͪΌΜͱ࣮͢Εྫ͑ʮmemcached ͕յ Εͯmemcached ʹؔͳ͍ͱ͜ΖʹҰ Өڹ͕Ͱͳ͍ʯΑ͏ͳϑΣʔϧιϑτͳγε ςϜΛ࡞Γ্͛ΒΕΔ