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
500
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
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
470
SSMRunbook作成の勘所_20241120
koichiotomo
2
130
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
170
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
120
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
210
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
5
590
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
Can We Measure Developer Productivity?
ewolff
1
150
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
950
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
240
ドメイン名の終活について - JPAAWG 7th -
mikit
33
20k
強いチームと開発生産性
onk
PRO
33
11k
Featured
See All Featured
A better future with KSS
kneath
238
17k
How GitHub (no longer) Works
holman
310
140k
A designer walks into a library…
pauljervisheath
203
24k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Rails Girls Zürich Keynote
gr2m
94
13k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Embracing the Ebb and Flow
colly
84
4.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/