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
2k
つまらないシステム(1) Scala の Future と ExecutionContext
Scalaで詰まらないシステムを作るために覚えていてほしいこと。
mashijp
November 13, 2015
Tweet
Share
More Decks by mashijp
See All by mashijp
幅広い環境でSSL対応するために知っておくべきこと
mashijp
7
2.4k
SSL勉強会 - 「クラウドを支えるこれからの暗号技術」を読んで
mashijp
0
440
つまらないシステム(2) - Scala 書きやすすぎるFutureの罠
mashijp
0
58
Other Decks in Programming
See All in Programming
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
920
Let's learn code review
riofujimon
2
620
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
140
2024 コーディング研修
ckazu
0
400
Ruby GitHub Packages
bkuhlmann
0
650
Elm 0.19.0 Changes
bkuhlmann
0
510
Direct Style Effect Systems The Print[A] ExampleA Comprehension Aid
philipschwarz
PRO
0
290
slow types ってなんだろう?
karad
0
170
PHPはいつから死んでいるかの調査
chiroruxx
2
430
新宿ダンジョンを可視化してみた
satoshi7190
3
420
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
290
Next.js App Router
quramy
12
2.1k
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
Art, The Web, and Tiny UX
lynnandtonic
290
19k
The Cult of Friendly URLs
andyhume
74
5.7k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
22
1.6k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
The Straight Up "How To Draw Better" Workshop
denniskardys
228
130k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
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 ʹؔͳ͍ͱ͜ΖʹҰ Өڹ͕Ͱͳ͍ʯΑ͏ͳϑΣʔϧιϑτͳγε ςϜΛ࡞Γ্͛ΒΕΔ