Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ReactPHPとの戯れ

 ReactPHPとの戯れ

PHPカンファレンス福岡2018 前夜祭リジェクトコン #phpconfuk_rej

s-ichikawa

June 15, 2018
Tweet

More Decks by s-ichikawa

Other Decks in Technology

Transcript

  1. ReactPHP? • ϊϯϒϩοΩϯάI/OɺΠϕϯτۦಈܕϓϩάϥϛϯάΛՄೳʹ͢ΔҝͷϥΠϒϥϦ • جຊతʹಛผͳ֦ுϞδϡʔϧΛඞཁͱ͠ͳ͍ • Core Components • EventLoop

    ΠϕϯτΛ؂ࢹͯ͠ɺରԠ͢ΔॲཧΛ࣮ߦ͢ΔͨΊͷϧʔϓ • Stream ϊϯϒϩοΩϯάI/OΛ࣮ݱ͢ΔͨΊͷίϯϙʔωϯτ • Promise Promise/A ࣮૷ΛՄೳʹ͢Δίϯϙʔωϯτ • Network Components (Socket, Datagram) • Protocol Components (HTTP, HTTP Client, DNS)
  2. I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱ଴ͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •

    I/Oର৅ͷfd͕ૢ࡞Ͱ͖ͳ͍৔߹͸ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·Ͱͷؒ͸CPU͸ผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴଴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕׬ྃͨ͠λΠϛϯάͰ௨஌͞ΕΔɻ • ΞϓϦέʔγϣϯ͸ͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧ͸εϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
  3. I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱ଴ͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •

    I/Oର৅ͷfd͕ૢ࡞Ͱ͖ͳ͍৔߹͸ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·Ͱͷؒ͸CPU͸ผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴଴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕׬ྃͨ͠λΠϛϯάͰ௨஌͞ΕΔɻ • ΞϓϦέʔγϣϯ͸ͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧ͸εϨου౳Ͱॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
  4. I/Oͷछྨ • ಉظ(ϒϩοΩϯά) I/O • ϑΝΠϧσΟεΫϦϓλ(fd)͕ૢ࡞ग़དྷΔঢ়ଶʹͳΔ·Ͱ଴ͪଓ͚Δɻ • ϊϯϒϩοΩϯά I/O •

    I/Oର৅ͷfd͕ૢ࡞Ͱ͖ͳ͍৔߹͸ΤϥʔΛฦ͠ɺఆظతʹϦτϥΠΛߦ͏ɻ • ϦτϥΠ͢Δ·Ͱͷؒ͸CPU͸ผͷλεΫΛߦ͏͜ͱ͕Ͱ͖Δɻ • ड৴଴͕ͪൃੜ͢ΔωοτϫʔΫI/OΛѻ͏͕ಘҙͱ͍ΘΕ͍ͯΔϞσϧɻ • ඇಉظ I/O • I/O͕׬ྃͨ͠λΠϛϯάͰ௨஌͞ΕΔɻ • ΞϓϦέʔγϣϯ͸ͦͷؒଞͷॲཧΛߦ͏͜ͱ͕Ͱ͖Δɻ • ඇಉظʹͨ͠ॲཧ͸εϨουͰॲཧ͢ΔͨΊϝϞϦΛফඅ͕ͪ͠ɻ
  5. I/OଟॏԽ • select(), poll(), epoll()ͳͲͷγεςϜίʔϧΛ࢖ͬͯγϯάϧϓϩηεͰෳ਺ͷ fdͷঢ়ଶมԽΛ؂ࢹ͢Δɻ • γεςϜίʔϧ΍ϥΠϒϥϦ • select()ɿ࠷΋ҰൠతͳγεςϜίʔϧɻpollʹൺ΂Δͱଟ͘ͷOSͰ࣮૷͞

    Ε͍ͯΔͷͰҠ২ੑߴΊɻ • poll()ɿselect()ΑΓଟػೳˍগ͠ੑೳ͕Α͍ɻ • epoll() (Linux2.6~), kqueue(BSD), /dev/poll(Solaris)ɿ্هͷߋʹߴੑೳ൛ • libevɿ؀ڥ͝ͱʹ࠷దͳγεςϜίʔϧબΜͰ͘ΕΔ
  6. ResourceStreamͷΠϕϯτ • ReadableResourceSream
 data: σʔλΛಡΈࠐΜͩ࣌ʹൃੜ
 error: ಡΈࠐΈʹࣦഊͨ࣌͠ʹൃੜ
 end: σʔλΛಡΈࠐΈऴΘͬͨ࣌ʹൃੜ
 close:

    stream͕ดͨ͡ͱ͖ʹൃੜ • WritableResourceStream
 drain: ॻ͖ࠐΈόοϑΝ͕์ग़͞Εͨ࣌ʹൃੜ
 pipe: readableετϦʔϜ͔Βpipe()ͰσʔλΛड৴ͨ͠ͱ͖ʹൃੜ
 error: ॻ͖ࠐΈ͕ࣦഊͨ͠ͱ͖ʹൃੜ
 close: streamΛด͡Δͱ͖ʹൃੜ
  7. ४උˍ֓ཁ • 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
  8. Learning Event-Driven PHP With ReactPHP w ͓ͦΒ͘།Ұͷ3FBDU1)1ͷॻ ੶ w -FBO1VC·ͨ͸,JOEMFͰൃചத


    IUUQTMFBOQVCDPNFWFOU ESJWFOQIQ
 IUUQTXXXBNB[PODPKQ EQ#:49'