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
480
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
86
SendGridで人生変わった
ichikawa
2
2.2k
Laravel with SendGrid
ichikawa
0
2.8k
Other Decks in Technology
See All in Technology
リスクから学ぶKubernetesコンテナセキュリティ/k8s-risk-and-security
mochizuki875
1
320
Develop to Survive - YAPC::Hakodate 2024 Keynote
moznion
8
2.2k
プロダクト開発の貢献をアピールするための目標設計や認知活動 / Goal design and recognition activities to promote product development contributions.
oomatomo
3
560
Hazard pointers with reference counter
ennael
PRO
0
120
成果のためのコミュニケーション - 語彙を育てよう -/communication-for-good-outcome-developing-vocabulary
hassaku63
4
160
スクラム導入の舞台裏:QAエンジニアがスクラムマスターになるまで
bubo1201
0
180
入門 KRR
donkomura
0
110
Assisted reorganization of data structures
ennael
PRO
0
250
AWS Lambdaで実現するスケーラブルで低コストなWebサービス構築/YAPC::Hakodate2024
fujiwara3
7
3.1k
【shownet.conf_】AI技術とUX監視の応用でShowNetの基盤を支えるモニタリングシステム
shownet
PRO
0
370
All your memory are belong to… whom?
ennael
PRO
0
650
トークナイザー入門
payanotty
2
940
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Designing the Hi-DPI Web
ddemaree
279
34k
In The Pink: A Labor of Love
frogandcode
139
22k
We Have a Design System, Now What?
morganepeng
49
7.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Making Projects Easy
brettharned
115
5.8k
No one is an island. Learnings from fostering a developers community.
thoeni
19
2.9k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
A Tale of Four Properties
chriscoyier
156
22k
Designing for Performance
lara
604
68k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.5k
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/