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. About me • Software Engineer and Architect • Twitter: @doubaokun

    • Email: [email protected] • Github: github.com/doubaokun • Application performance optimization • Realtime data processing system • Infrastructure
  2. • PHP (7) • Asynchoronous & non-blocking IO • Asynchoronous

    PHP • PHP Extension and PHP server • Swoole: Async programming framework Summary
  3. PHP

  4. Why PHP As of 2017, PHP comprises more than 80%

    of the websites on the Internet.
  5. Is

  6. 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
  7. 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
  8. 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
  9. 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
  10. 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.
  11. • Node.js • Netty (Java) • Twisted (Python) • Ruby

    (EventMachine) • Akka (Scala) • Swoole (PHP) Reactor pattern
  12. 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
  13. 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
  14. 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
  15. 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