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

Preemptive multitasking using coroutines in PHP Swoole

Cc3b97b64109e493f51bc2a91a426f97?s=47 Swoole
June 06, 2019

Preemptive multitasking using coroutines in PHP Swoole

Cc3b97b64109e493f51bc2a91a426f97?s=128

Swoole

June 06, 2019
Tweet

More Decks by Swoole

Other Decks in Programming

Transcript

  1. Preemptive multitasking using coroutines in PHP Swoole Bruce Dou |

    @doubaokun bruce@transfon.com 6th Jun 2019
  2. 1. Evolution of web server or services 2. The problems

    to solve for PHP 3. Coroutinesand preemptive schedulingfor PHP 4. Show case preemptivescheduling in PHP Swoole Agenda
  3. About me • Software Architect & Head of Development •

    Founderat Transfon • Twitter:@doubaokun • Email:bruce@transfon.com • Linkedin:linkedin.com/in/brucedou • Github: github.com/doubaokun
  4. Security Performance Regulation compliance Cost control About Transfon

  5. Evolution of web server or services

  6. None
  7. https://unixism.net/2019/04/linux-applications-performance-introduction/

  8. I/O bound vs CPU bound

  9. Decoupling I/O and Computation

  10. Async and non-blocking I/O

  11. Why non-blocking I/O matters Multiple cores and fast CPU vs

    slow Network I/O
  12. Linux I/O multiplexing, non-blocking • select() monitors the sockets’ status

    flag and return the status of all sockets, manually iterate through all the fds (C10K problem) • poll() similar to select() but allocate an array of pollfd, can handle more than 1024 sockets •epoll() monitors sockets’ status and trigger related events, and only return triggered events array.
  13. Reactor pattern

  14. PHP ?

  15. The problems to solve for PHP • High concurrency,large numberof

    connections • Long-liveconnections,real-timeservices • Long-liveconnectionsto DB, Cache, RPC etc • Supportmoreprotocols • Server programming,not just scripts • Milliseconds scheduler • Async tasks of consuming3rd party services • Manage and reuse the statusin memory • Performance
  16. None
  17. 1.Receive the request 2.Load and compile PHP files and codes

    3.Initialize the context objects and variables 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP-FPM
  18. 1.Load and compile files and codes 2.Initialize the context objects

    and variables 3.Receive the request 4.Execute the functions 5.Send the response 6.Recycle the resources The life cycle of HTTP request in PHP Swoole
  19. PHP Swoole https://www.swoole.co.uk/ https://github.com/swoole/swoole-src @php_swoole Coroutine-based concurrency library for PHP

  20. Get started git clone git@github.com:doubaokun/phplondon201906.git docker build -f Dockerfile -t

    swoole-phplondon . docker run --rm -p 9501:9501 -v $(pwd):/app -w /app \ swoole-phplondon server.php
  21. How it looks like

  22. How fast

  23. Open Source(Apache2 license) PHP Extension C/C++ / Boost.Context Callbackstyle &

    Coroutine Event-basednon-blockingI/O HTTP/HTTP2/Websocket/TCP/UDPetc Milliseconds task scheduler Process manager Preemptivescheduling PHP Swoole
  24. The problems we like to solve further • Callback hell

    • Use Coroutinestyle (sync) concurrency • Automaticallycontextswitchbasedon I/O • Soft real-time • Long tail latency • One bad guy stopthe world
  25. Callback hell

  26. Callback hell Swoole 2.x.x

  27. I/O based automatically scheduling in PHP Swoole

  28. I/O based automatically scheduling

  29. Coroutines in PHP Swoole

  30. Coroutines in PHP Swoole

  31. Problem with CPU heavy business logics

  32. Preemptive scheduling

  33. PHP Swoole Server

  34. HTTP server with PHP Swoole

  35. None
  36. HTTP server structure

  37. Preemptive scheduling in PHP Swoole Server

  38. Scheduling thread ZendVM:EG(vm_interrupt)= 1 Zend_interrupt_function Yield current coroutineif it runs

    longer than 5ms How preemptive scheduling works
  39. Preemptive scheduling in PHP Swoole Server

  40. HTTP server structure single process

  41. Preemptive scheduling in PHP Swoole Server time curl -I http://127.0.0.1:9501/\?p\=1

    (CPU heavy logics) time curl -I http://127.0.0.1:9501/\?p\=0 (light weight logics) docker run --rm -p 9501:9501 -v $(pwd):/app -w /app \ swoole-phplondon server_preemptive.php time curl -I http://127.0.0.1:9501/\?p\=1 & sleep 1 && \ time curl -I http://127.0.0.1:9501/\?p\=0 & wait
  42. Preemptive scheduling in PHP Swoole Server ini_set("swoole.enable_preemptive_scheduler", "0");

  43. Preemptive scheduling in PHP Swoole Server ini_set("swoole.enable_preemptive_scheduler", "1");

  44. Real world meaningof preemptive scheduling

  45. Real-world meaning of preemptive scheduling

  46. https://github.com/doubaokun/phplondon201906 https://www.swoole.co.uk/ https://github.com/swoole/swoole-src https://swoole.slack.com https://twitter.com/php_swoole Thank you