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

Swooleで実装するPHP非同期処理の世界/phpcondo2019-swoole

KOYAMA Tetsuji
September 21, 2019

 Swooleで実装するPHP非同期処理の世界/phpcondo2019-swoole

2019-09-21 に開催されたPHPカンファレンス北海道2019で発表した資料です。

KOYAMA Tetsuji

September 21, 2019
Tweet

More Decks by KOYAMA Tetsuji

Other Decks in Programming

Transcript

  1. 4XPPMFͰ࣮૷͢Δ
    1)1ඇಉظॲཧͷੈք
    খࢁ఩ࢤ
    !LPZIPHF
    PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21

    View full-size slide

  2. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ࣗݾ঺հ
    খࢁ఩ࢤ ͜΍·ͯͭ͡

    ߹ಉձࣾ΄ٕ͛ݚ
    Ϣʔβձ͍͔ͭ͘
    ೔ຊ6/*9Ϣʔβձ
    ೔ຊ1PTUHSF42-Ϣʔβձ
    ೔ຊ1)1Ϣʔβձ
    SBLVNPגࣜձࣾΤϯδχΞ
    !LPZIPHF LPZIPHF

    View full-size slide

  3. ʮখࢁ͞Μ͸
    ɹͲ͜ʹͰ΋͍·͢Ͷʯ

    View full-size slide

  4. ʮͲ͜ʹͰ΋͸͍ͳ͍Θɺ
    ͍Δͱ͜Ζʹ͚ͩʯ

    View full-size slide

  5. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ͯ͞
    4XPPMF

    View full-size slide

  6. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ʜͷલʹ

    View full-size slide

  7. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ࣮ߦ଎౓ͷ௿ԼཁҼ
    w$16
    wϝϞϦ
    wϩδοΫ
    wσʔλॲཧྔ
    w*0


    View full-size slide

  8. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    *0
    w$16ॲཧʹൺ΂Δͱஈҧ͍ʹ஗͍
    wऴྃ଴ͪঢ়ଶ
    wಉظॲཧ

    View full-size slide

  9. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    Gregg, Brendan. “Systems Performance: Enterprise and the Cloud.”
    *0஗Ԇ

    View full-size slide

  10. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ඇಉظ*0
    w*0ॲཧͷऴྃΛ଴ͨͳ͍
    wϒϩοΫ͠ͳ͍
    wͦͷؒʹଞͷॲཧΛ͢Δ
    w*0ॲཧ͕ऴΘͬͨΒͦͪΒʹ໭Δ

    View full-size slide

  11. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    https://twitter.com/php_swoole/status/1060181279407169538

    View full-size slide

  12. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    1)14XPPMF
    $PSPVUJOFCBTFEDPODVSSFODZMJCSBSZGPS1)1
    https://www.swoole.co.uk/
    https://github.com/swoole/swoole-src/
    @php_swoole

    View full-size slide

  13. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    4XPPMFͱ͸
    wඇಉظॲཧΛجຊʹͨ͠ߴ଎Խॲཧͷ
    ٧Ί߹ΘͤϥΠϒϥϦ
    w೥͔Β։ൃ
    wݩ͸ӳࠃ5SBOTGPOʹΑͬͯ
    wݱࡏ͸தࠃݍͷ։ൃऀ͕໠྽ʹ

    View full-size slide

  14. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    1)14XPPMF
    w Φʔϓϯιʔε "QBDIFϥΠηϯε

    w 1)1&YUFOTJPO
    w $$#PPTU$POUFYU
    w ίʔϧόοΫίϧʔνϯ
    w Πϕϯτۦಈͷඇಉظ*0
    w )551)5518FC4PDLFU5$16%1
    w ϚΠΫϩඵλεΫεέδϡʔϥ
    w ϓϩηεϚωʔδϟ
    w ϓϦΤϯϓςΟϒεέδϡʔϦϯά

    View full-size slide

  15. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    Πϯετʔϧ
    $ pecl install swoole
    extension=swoole.so
    ZPVSJOJ

    View full-size slide

  16. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ࠷ॳͷҰา
    $ php co.php
    start coro 1
    start to resume 1 @1
    resume coro 1 @1
    start to resume 1 @2
    resume coro 1 @2
    main

    View full-size slide

  17. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ղઆ
    w4XPPMFa$PSPVUJOFʹ͸$Pͱ͍͏ΤΠϦ
    Ξε͕͍͍ͭͯΔ
    w࠷ॳͷVTFจ͸ͳͯ͘΋ಈ͘
    w1)1͸Ϋϥε໊ͷେখจࣈ͸۠ผ͠ͳ͍
    wHP
    ͸4XPPMFa$PSPVUJOFDSFBUF
    ͷΤ
    ΠϦΞε

    View full-size slide

  18. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ղઆ
    wDPZJFME
    ͸ॲཧΛҰ୴தஅ͢Δ
    wDPSFTVNF
    ͸ର৅͕ZJFME
    ͢Δ·Ͱϒϩο
    Ϋ͢Δ

    View full-size slide

  19. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    4XPPMFίϧʔνϯ
    w4XPPMFͷඇಉظॲཧͷࠜװ
    wதஅ࠶։Ͱ͖Δॲཧ୯Ґ
    w4XPPMFͰ͸γϯάϧεϨουͰ࣮૷
    ͞Ε͍ͯΔ

    View full-size slide

  20. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    swoole-src/include/coroutine.h
    ࣮૷ͷத֩෦෼

    View full-size slide

  21. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ίϧʔνϯྫ
    $ php co2.php
    write...: data/00.out
    write...: data/01.out
    write...: data/02.out
    write...: data/03.out
    write...: data/04.out
    write: data/00.out done
    write: data/01.out done
    write: data/02.out done
    write: data/03.out done
    write: data/04.out done

    View full-size slide

  22. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    EFGFS
    wίϧʔνϯͷऴྃॲཧ
    wίϧʔνϯ͕FYJU͢Δ·Ͱ࣮ߦ͞Εͳ
    ͍
    w࠷ޙʹݺ͹ΕͨEFGFS͔Βٯॱʹ࣮ߦ
    ͞ΕΔ

    View full-size slide

  23. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    EFGFSͷྫ
    $ php defer.php
    abc~b~a

    View full-size slide

  24. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    DIBO
    wνϟϯωϧΛ࡞੒͢Δ
    wίϧʔνϯؒͷϝοηʔδड͚౉͠

    View full-size slide

  25. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    DIBOͷྫ

    View full-size slide

  26. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    https://speakerdeck.com/team/swoole-4-introducing-the-new-coroutine-design-pattern-in-php?slide=26

    View full-size slide

  27. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    4XPPMF4FSWFS
    w)551
    w)551
    w8FC4PDLFU
    w5$16%16OJY4PDLFU
    w3FEJT

    View full-size slide

  28. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    )5514FSWFS

    View full-size slide

  29. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    5$14FSWFS

    View full-size slide

  30. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    ·ͱΊ
    w4XPPMF͸1)1ߴ଎Խͷ৽͍͠ํ޲ੑ
    wඇ࿈ଓతਐԽ
    w׆ൃʹ։ൃ͞Ε͍ͯΔ༷ࢠΛோΊ͍ͯ
    Δ͚ͩͰ΋ָ͍͠
    wʮ1)1Ͱ͜͜·ͰͰ͖Δʯͱূ໌ͨ͠
    ͜ͱʹܟҙΛධ͍ͨ͠

    View full-size slide

  31. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    એ఻
    w ݄຤ʹٕज़ධ࿦ࣾΑΓʮΈΜͳͷ1)1ʯ͕ग़·͢
    w 1)1ΛςʔϚʹͨ͠ϜοΫ
    w ϖʔδલޙ
    w Ձ֨ະఆ
    w 4PGUXBSF%FTJHO೥݄߸ ൃച

    w λΠΞοϓಛू1
    w 8%#13&44WPM ൃച

    w 4XPPMFʹؔ͢ΔλΠΞοϓهࣄ1

    View full-size slide

  32. PHPΧϯϑΝϨϯε๺ւಓ2019 2019-09-21
    3FG
    w IUUQTTXPPMFDPVL
    w IUUQTHJUIVCDPNTXPPMFTXPPMFTSD
    w IUUQTTQFBLFSEFDLDPNUFBNQSFFNQUJWF
    NVMUJUBTLJOHVTJOHDPSPVUJOFTJOQIQTXPPMF
    w IUUQTTQFBLFSEFDLDPNUFBNTXPPMF
    JOUSPEVDJOHUIFOFXDPSPVUJOFEFTJHOQBUUFSOJO
    QIQ

    View full-size slide