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

Asynchronous programming with PHP

Bruce Dou
October 18, 2017

Asynchronous programming with PHP

* Why and How to utilize asynchronous in PHP
* How to implement a simple asynchronous server with PHP C extension
* Production ready, large-scale asynchronous programming with PHP language and Swoole

Bruce Dou

October 18, 2017
Tweet

More Decks by Bruce Dou

Other Decks in Technology

Transcript

  1. Asynchronous
    programming with PHP
    Bruce (Baokun) Dou
    @doubaokun
    18th Oct 2017

    View Slide

  2. About me
    • Software Engineer and Architect
    • Twitter: @doubaokun
    • Email: [email protected]
    • Github: github.com/doubaokun
    • Application performance optimization
    • Realtime data processing system
    • Infrastructure

    View Slide

  3. #APO
    Tracelytics / Appneta, 2015

    View Slide

  4. Transfon.com


    APO => Cost reduction


    Saving the ?

    View Slide

  5. I/O bound vs CPU bound

    View Slide

  6. Communication vs Computation

    View Slide

  7. • PHP (7)
    • Asynchoronous & non-blocking IO
    • Asynchoronous PHP
    • PHP Extension and PHP server
    • Swoole: Async programming framework
    Summary

    View Slide

  8. PHP

    View Slide

  9. Why PHP
    As of 2017, PHP comprises more than 80% of the websites on the Internet.

    View Slide

  10. The performance of PHP 7

    View Slide

  11. Is

    View Slide

  12. Awesome

    View Slide

  13. Traditional PHP model
    • LNMP (LAMP): Linux, Nginx, MySQL, PHP-FPM
    • Originally created as a templating language
    • Thread/process based model
    • Blocking IO and Sync
    • Stateless and scalable
    • FastCGI
    • Process per connection is memory expensive
    • Build up everything then destroy everything
    • Triggered by user request (No scheduler)
    • Process per connection is easy for 504 timeout

    View Slide

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

    View Slide

  15. Pros and Cons of PHP
    Pros Cons
    Simple and easy No IoT (asynchronous I/O)
    Huge Ecosystem Difficult to implement Server side
    protocol and services: TCP Socket,
    SMTP, MQTT, WebSocket, Thrift, etc
    Sync
    Simple and easy
    Sync
    One wait stop all
    Stateless Stateless
    Scalable Low performance
    Robust No long-live connection
    No pooling

    View Slide

  16. Async and non-blocking I/O

    View Slide

  17. Sync vs Async
    http://mikecantelon.com/talks/trijug/preso.html#14

    View Slide

  18. How to do async tasks
    Message Queue or Task Queue
    Restful services or RPC
    Ajax
    CURL
    Fork a new process/thread
    CRON
    vs
    I/O multiplexing, Non-blocking I/O, IPC

    View Slide

  19. Why non-blocking I/O matters
    Multiple cores and fast CPU vs slow Network I/O

    View Slide

  20. Why non-blocking I/O matters
    Multiple cores and fast CPU vs slow Network I/O

    View Slide

  21. 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.

    View Slide

  22. Decoupling IO and Computation

    View Slide

  23. libevent

    View Slide

  24. libuv

    View Slide

  25. Reactor pattern

    View Slide

  26. Reactor pattern

    View Slide

  27. • Node.js
    • Netty (Java)
    • Twisted (Python)
    • Ruby (EventMachine)
    • Akka (Scala)
    • Swoole (PHP)
    Reactor pattern

    View Slide

  28. Async vs parallel
    Responsiveness
    vs
    Performance

    View Slide

  29. Async PHP

    View Slide

  30. The problems we like to sort out for PHP
    • High concurrency, large number of connections
    • Long-live connections, real-time services
    • Long-live connections to DB, Cache, RPC etc
    • Support more server side protocols
    • Server side programming
    • Scheduler
    • Async tasks need 3rd party services
    • Manage and reuse the status in memory
    • Performance

    View Slide

  31. Async PHP server side options
    • ReactPHP
    • Workerman
    • Swoole

    View Slide

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

    View Slide

  33. 1.Load and compile files and codes
    2.Initialize the 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

    View Slide

  34. Not a replacement
    for 80% PHP web applications

    View Slide

  35. But it opens the door for PHP developers:
    • RPC server
    • Websocket server
    • TCP server
    • High performance services
    • High concurrency systems (C100K)
    • Internet of things server
    • Compete with language X, Y, Z

    View Slide

  36. PHP Extension

    View Slide

  37. PHP Extension

    View Slide

  38. Demo
    https://github.com/doubaokun/rxserver

    View Slide

  39. Swoole
    https://www.swoole.co.uk/
    High performance, Open source,
    Production ready async programming framework for PHP
    @php_swoole

    View Slide

  40. https://gist.github.com/nkt/e49289321c744155484c

    View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. View Slide

  46. View Slide

  47. PSR-7 HTTP Message Interfaces
    http://symfony.com/blog/psr-7-support-in-symfony-is-here
    https://www.drupal.org/node/2492955
    https://laravel.com/docs/5.1/requests

    View Slide

  48. Not the End

    View Slide

  49. Create something awesome with Swoole

    View Slide