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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
mashijp
November 13, 2015
Programming
2.6k
0
Share
つまらないシステム(1) Scala の Future と ExecutionContext
Scalaで詰まらないシステムを作るために覚えていてほしいこと。
mashijp
November 13, 2015
More Decks by mashijp
See All by mashijp
幅広い環境でSSL対応するために知っておくべきこと
mashijp
7
3.1k
SSL勉強会 - 「クラウドを支えるこれからの暗号技術」を読んで
mashijp
0
490
つまらないシステム(2) - Scala 書きやすすぎるFutureの罠
mashijp
0
75
Other Decks in Programming
See All in Programming
「話せることがない」を乗り越える 〜日常業務から登壇テーマをつくる思考法〜
shoheimitani
4
820
AIエージェントで業務改善してみた
taku271
0
530
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
430
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.6k
Back to the roots of date
jinroq
0
100
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
150
PCOVから学ぶコードカバレッジ #phpcon_odawara
o0h
PRO
0
270
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
220
Vibe NLP for Applied NLP
inesmontani
PRO
0
440
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
0
140
Kubernetes上でAgentを動かすための最新動向と押さえるべき概念まとめ
sotamaki0421
3
510
Liberating Ruby's Parser from Lexer Hacks
ydah
2
1.3k
Featured
See All Featured
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
230
Speed Design
sergeychernyshev
33
1.6k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
160
Building Applications with DynamoDB
mza
96
7k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
280
How to Talk to Developers About Accessibility
jct
2
180
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
440
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
160
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Bash Introduction
62gerente
615
210k
Practical Orchestrator
shlominoach
191
11k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
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 ʹؔͳ͍ͱ͜ΖʹҰ Өڹ͕Ͱͳ͍ʯΑ͏ͳϑΣʔϧιϑτͳγε ςϜΛ࡞Γ্͛ΒΕΔ