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
ReactPHPとの戯れ
Search
s-ichikawa
June 15, 2018
Technology
0
510
ReactPHPとの戯れ
PHPカンファレンス福岡2018 前夜祭リジェクトコン #phpconfuk_rej
s-ichikawa
June 15, 2018
Tweet
Share
More Decks by s-ichikawa
See All by s-ichikawa
15分で分かった気になるGraphQL
ichikawa
3
3.1k
GraphQL入門
ichikawa
3
1.1k
商品監視を支える技術.key.pdf
ichikawa
0
87
SendGridで人生変わった
ichikawa
2
2.2k
Laravel with SendGrid
ichikawa
0
2.8k
Other Decks in Technology
See All in Technology
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
210
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
730
MLOps の現場から
asei
6
630
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
330
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
170
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
550
ガバメントクラウドのセキュリティ対策事例について
fujisawaryohei
0
530
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
470
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
150
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
9k
Music & Morning Musume
bryan
46
6.2k
Designing for Performance
lara
604
68k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Embracing the Ebb and Flow
colly
84
4.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Typedesign – Prime Four
hannesfritz
40
2.4k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Automating Front-end Workflow
addyosmani
1366
200k
Transcript
ReactPHPͱͷٔΕ PHPΧϯϑΝϨϯεԬ2018 લલࡇϦδΣΫτίϯ @ichikawa_0829
Twitter: @ichikawa_0829 ϝϧΧϦ CRE Backend Engineer PHPྺ 6
None
/PU1)1 3FBDUKT 1VSF1)1
ReactPHP?
ReactPHP? • ϊϯϒϩοΩϯάI/OɺΠϕϯτۦಈܕϓϩάϥϛϯάΛՄೳʹ͢ΔҝͷϥΠϒϥϦ • جຊతʹಛผͳ֦ுϞδϡʔϧΛඞཁͱ͠ͳ͍ • Core Components • EventLoop
ΠϕϯτΛࢹͯ͠ɺରԠ͢ΔॲཧΛ࣮ߦ͢ΔͨΊͷϧʔϓ • Stream ϊϯϒϩοΩϯάI/OΛ࣮ݱ͢ΔͨΊͷίϯϙʔωϯτ • Promise Promise/A ࣮ΛՄೳʹ͢Δίϯϙʔωϯτ • Network Components (Socket, Datagram) • Protocol Components (HTTP, HTTP Client, DNS)
ϊϯϒϩοΩϯάI/Oʁ Πϕϯτۦಈʁʁ
I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •
I/Oରͷfd͕ૢ࡞Ͱ͖ͳ͍߹ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·ͰͷؒCPUผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕ྃͨ͠λΠϛϯάͰ௨͞ΕΔɻ • ΞϓϦέʔγϣϯͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧεϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
ಉظ(ϒϩοΩϯά) I/O https://www.ibm.com/developerworks/library/l-async/index.html
ΠΧ৯͍ͨ ͖ͬͯ·ʔ͢ ·ͩΕͳ͍࣌ظ ΕΔ࣌ظ Εͨʂ ঌ্͕͠Εʂ "QQMJDBUJPO ,FSOFM GE
I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •
I/Oରͷfd͕ૢ࡞Ͱ͖ͳ͍߹ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·ͰͷؒCPUผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕ྃͨ͠λΠϛϯάͰ௨͞ΕΔɻ • ΞϓϦέʔγϣϯͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧεϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
Non Blocking I/O https://www.ibm.com/developerworks/library/l-async/index.html
ΠΧ৯͍ͨ ͖ͬͯ·ʔ͢ ·ͩΕͳ͍࣌ظ ΕΔ࣌ظ Εͨʂ ঌ্͕͠Εʂ ࠓΕͳ͍ ਐḿͲ͏Ͱ͔͢ ਐḿμϝͰ͢ Εͨʁ
I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •
I/Oରͷfd͕ૢ࡞Ͱ͖ͳ͍߹ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·ͰͷؒCPUผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕ྃͨ͠λΠϛϯάͰ௨͞ΕΔɻ • ΞϓϦέʔγϣϯͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧεϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
ඇಉظ(Asynchronous) I/O https://www.ibm.com/developerworks/library/l-async/index.html
ΠΧ৯͍͔ͨΒ ΕͨΒڭ͑ͯ ͖ͬͯ·ʔ͢ ·ͩΕͳ͍࣌ظ ΕΔ࣌ظ Εͨʂ ঌ্͕͠Εʂ
Πϕϯτۦಈܕϓϩάϥϛϯά • ΠϕϯτͷࢹΛߦ͍ɺൃੜͨ͠ΠϕϯτʹैͬͯॲཧΛ࣮ߦ͢Δ • ΠϕϯτʹϚεΩʔϘʔυͷૢ࡞ɾೖྗɺιέοτ͔Βͷϝοηʔ δͷड৴ͳͲ৭ʑ͋Δ • ͦΕΒͷΠϕϯτΛػ͢Δϧʔϓͷ͜ͱΛΠϕϯτϧʔϓͱݺͼɺΠϕϯ τ͕ൃੜ͔ͨ͠ϑΝΠϧσΟεΫϦϓλιέοτσΟεΫϦϓλΛ ࢹ͢Δ
• ReactPHPNode.jsI/OͷଟॏԽ(I/O multiplexing)Λߦ͍ෳͷσΟεΫϦϓλ ͷࢹΛՄೳʹ͍ͯ͠Δ
I/OଟॏԽ • select(), poll(), epoll()ͳͲͷγεςϜίʔϧΛͬͯγϯάϧϓϩηεͰෳͷ fdͷঢ়ଶมԽΛࢹ͢Δɻ • γεςϜίʔϧϥΠϒϥϦ • select()ɿ࠷ҰൠతͳγεςϜίʔϧɻpollʹൺΔͱଟ͘ͷOSͰ࣮͞
Ε͍ͯΔͷͰҠ২ੑߴΊɻ • poll()ɿselect()ΑΓଟػೳˍগ͠ੑೳ͕Α͍ɻ • epoll() (Linux2.6~), kqueue(BSD), /dev/poll(Solaris)ɿ্هͷߋʹߴੑೳ൛ • libevɿڥ͝ͱʹ࠷దͳγεςϜίʔϧબΜͰ͘ΕΔ
Hello World
Hello World
Start Event Loop in ReactPHP
Hello World
ResourceStreamͷΠϕϯτ • ReadableResourceSream data: σʔλΛಡΈࠐΜͩ࣌ʹൃੜ error: ಡΈࠐΈʹࣦഊͨ࣌͠ʹൃੜ end: σʔλΛಡΈࠐΈऴΘͬͨ࣌ʹൃੜ close:
stream͕ดͨ͡ͱ͖ʹൃੜ • WritableResourceStream drain: ॻ͖ࠐΈόοϑΝ͕์ग़͞Εͨ࣌ʹൃੜ pipe: readableετϦʔϜ͔Βpipe()ͰσʔλΛड৴ͨ͠ͱ͖ʹൃੜ error: ॻ͖ࠐΈ͕ࣦഊͨ͠ͱ͖ʹൃੜ close: streamΛด͡Δͱ͖ʹൃੜ
None
Hello World Server
४උˍ֓ཁ • composer require react/http • ϦΫΤετΛૹ৴͢ΔͱHello Worldͱฦ͚ͩ͢ͷαʔόΛ࡞Γ·͢
server.php
None
/PEFKTͱࣅͨײ͡Ͱ)FMMP8PSME4FSWFSΛ࡞Ͱ͖·ͨ͠ server.js
Making Simple Chat
४උˍ֓ཁ • composer require react/socket • React\Socket\ServerΫϥεΛ͍ɺSocket௨৴Λߦ͏ServerΛಈ͔͠ɺ Client͔ΒͷConnectionΛPool͠ɺϝοηʔδͷૹड৴Λߦ͍·͢ • ଓ༻ΫϥΠΞϯτReact\Socket\ConnectorΫϥεΛ༻
• https://github.com/s-ichikawa/react-php-chat
ଓ ໊લೖྗͯ͠Ͷ TJDIJLBXBͰ͢ Α͏ͦ͜ʂ TJDIJLBXB͕+PJO͠·ͨ͠
)FMMP8PSME TJDIJLBXB)FMMP8PSME
None
HTTP Request
४උˍ֓ཁ • composer require react/http-client • ςετ༻αʔόʹ1000ճϦΫΤετΛૹΔ $ time curl
127.0.0.1:8080 real 0m0.038s user 0m0.010s sys 0m0.013s • HttpΫϥΠΞϯτͱ͍͓͑ೃછΈͷGuzzlePHPͱൺֱͯ͠Έ·͢ ˞MBP, 2.9GHz Core i7, Memory 16GB
Guzzle UJNFQIQGHV[[MF@TJNQMF@TFOEFSQIQ SFBM NT VTFS NT TZT NT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/guzzle_simple_sender.php
ReactPHP UJNFQIQGSFBDU@CVML@TFOEFSQIQ SFBM NT VTFS NT TZTNT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/react_bulk_sender.php
Guzzle UJNFQIQGHV[[MF@QPPM@TFOEFSQIQ SFBM NT VTFS NT TZT NT https://github.com/s-ichikawa/react-example/blob/master/example/ HttpRequest/guzzle_pool_sender.php
• GuzzleHttp\ClientͰྻ࣮ߦ͢ΔΑΓReactPHPͷ΄͏͕ૣ͍ • GuzzleHttp\PoolͰϦΫΤετ͢Δ߹ѹతʹGuzzleͷউར • GuzzleඇಉظϦΫΤετ͢Δࡍɺ෦Ͱcurl-multiΛݺΜͰ͍Δɻcurl-multi pthreadΛ༻ͯ͠ฒߦॲཧ͍ͯ͠Δɻ • ReactPHP͋͘·ͰγϯάϧϓϩηεɾγϯάϧεϨου •
ReactPHPͱGuzzleͷAsyncXxx10000ճͱ͔࿈ଓͰϦΫΤετ͢Δͱ Too many open files ~ ͷΤϥʔʹͳͬͯ͠·͏߹͕͋ͬͨ
ͦͷଞ
• PHP-PM https://github.com/php-pm/php-pm • Async RedisΫϥΠΞϯτ https://github.com/nrk/predis-async • υϩʔϯඈͤΔ https://youtu.be/i07m_TFR9no?t=8m16s
None
https://youtu.be/i07m_TFR9no?t=8m16s
None
https://github.com/reactphp/react/wiki/Users
Learning Event-Driven PHP With ReactPHP w ͓ͦΒ͘།Ұͷ3FBDU1)1ͷॻ ੶ w -FBO1VC·ͨ,JOEMFͰൃചத
IUUQTMFBOQVCDPNFWFOU ESJWFOQIQ IUUQTXXXBNB[PODPKQ EQ#:49'
·ͱΊ • ϊϯϒϩοΩϯάI/O͕׆͖Δ໘ͰͬͯΈΔͷྑͦ͞͏ • PHPͷΈͰಈͨ͘ΊI/OपΓͷॲཧΛվળ͍͚ͨ͠ͲɺGoNode.jsʹΓସ͑ ΒΕͳ͍Α͏ͳ߹ʹࢼͯ͠ΈΔՁ͋Γͦ͏ • ΠϕϯτϧʔϓI/OͷछྨɾଟॏԽͷΈɺΠϕϯτۦಈͳͲීஈ͋· Γҙࣝͯ͠ͳ͔ͬͨ෦ΛษڧͰ͖ͨͷྑ͔ͬͨ •
ڵຯ͕༙͍ͨํͥͻAfter HackͰ࿔ͬͯΈ͍ͯͩ͘͞ PHP Conference Fukuoka After Hack!! https://fusic.connpass.com/event/83312/