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
0
2.5k
つまらないシステム(1) Scala の Future と ExecutionContext
Scalaで詰まらないシステムを作るために覚えていてほしいこと。
mashijp
November 13, 2015
Tweet
Share
More Decks by mashijp
See All by mashijp
幅広い環境でSSL対応するために知っておくべきこと
mashijp
7
2.9k
SSL勉強会 - 「クラウドを支えるこれからの暗号技術」を読んで
mashijp
0
480
つまらないシステム(2) - Scala 書きやすすぎるFutureの罠
mashijp
0
71
Other Decks in Programming
See All in Programming
AWS発のAIエディタKiroを使ってみた
iriikeita
1
180
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
430
テストカバレッジ100%を10年続けて得られた学びと品質
mottyzzz
2
590
為你自己學 Python - 冷知識篇
eddie
1
350
AIでLINEスタンプを作ってみた
eycjur
1
230
Navigating Dependency Injection with Metro
zacsweers
3
260
アセットのコンパイルについて
ojun9
0
120
Tool Catalog Agent for Bedrock AgentCore Gateway
licux
6
2.4k
MCPでVibe Working。そして、結局はContext Eng(略)/ Working with Vibe on MCP And Context Eng
rkaga
5
2.3k
rage against annotate_predecessor
junk0612
0
170
Namespace and Its Future
tagomoris
6
700
速いWebフレームワークを作る
yusukebe
5
1.7k
Featured
See All Featured
Gamification - CAS2011
davidbonilla
81
5.4k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
Six Lessons from altMBA
skipperchong
28
4k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Into the Great Unknown - MozCon
thekraken
40
2k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
The Power of CSS Pseudo Elements
geoffreycrofte
77
6k
The World Runs on Bad Software
bkeepers
PRO
70
11k
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 ʹؔͳ͍ͱ͜ΖʹҰ Өڹ͕Ͱͳ͍ʯΑ͏ͳϑΣʔϧιϑτͳγε ςϜΛ࡞Γ্͛ΒΕΔ