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

Preemptive multitasking using coroutines in PHP Swoole

Bruce Dou
June 06, 2019

Preemptive multitasking using coroutines in PHP Swoole

PHP Swoole enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP programming language.

Compared with other async programming frameworks or software such as Nginx, Tornado, Node.js, Swoole has the built-in async, coroutines, multiple threads I/O modules.

Preemptive scheduling is the key to building a high performance, low latency, near the real-time system.

Compared with other programming languages or frameworks, PHP Swoole is one of the very few supports preemptive multitasking using coroutines.

This talk we like to introduce the coroutines in PHP Swoole and the implementation of a multitasking coroutines scheduler in PHP Swoole.

Bruce Dou

June 06, 2019


  1. 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
  2. About me • Software Architect & Head of Development •

    Founderat Transfon • Twitter:@doubaokun • Email:[email protected] • Linkedin:linkedin.com/in/brucedou • Github: github.com/doubaokun
  3. 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.
  4. 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
  5. 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
  6. 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
  7. Get started git clone [email protected]: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
  8. 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
  9. 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
  10. Preemptive scheduling in PHP Swoole Server time curl -I\?p\=1

    (CPU heavy logics) time curl -I\?p\=0 (light weight logics) docker run --rm -p 9501:9501 -v $(pwd):/app -w /app \ swoole-phplondon server_preemptive.php time curl -I\?p\=1 & sleep 1 && \ time curl -I\?p\=0 & wait