How PHP Swoole works

PHP Swoole is different from the traditional PHP model, it is running in CLI mode more like Node.js but with a different design using coroutines without callback hell.

How PHP Swoole works

PHP Swoole internal

The differences between PHP Swoole with PHP-FPM the traditional PHP model are:

  • PHP Swoole supports TCP, UDP and UnixSocket
  • PHP Swoole uses Non-blocking I/O mode with epoll or kqueue
  • PHP Swoole forks several worker processes based on CPU core number to utilise all CPU cores.
  • PHP Swoole supports Long-live connections for WebSocket server or TCP/UDP server.
  • PHP Swoole supports more server-side protocols: TCP/UDP/HTTP/HTTP2/WebSocket
  • PHP Swoole preload PHP files into memory.
  • PHP Swoole can manage and reuse the status in memory with PHP Swoole Memory Management Modules.
  • Coroutine based concurrency compare with waterfall in PHP-FPM.

Swoole Server Structure

Master process

Master process is the entry point of Swoole Server.

Reactor threads in Master process

Reactor threads are created in Master process to handle client side network connection and network I\O.

Manager process

Manager process create or destory Worker process and Task Worker process.

Worker process

Worker process receive data from Reactor threads and execute business logics, then send back the response to Reactor threads.

Your application code should be run within Worker process.

Task Worker process

It is used to process blocking codes.

Task Worker process receive data from Worker process and send back the result to the Worker process.

A Worker process send data to Task worker process with: Swoole\Server->task/taskwait/taskCo/taskWaitMulti

A Task worker process send result back to Worker process with: Swoole\Server->finish

Interested with PHP Swoole? Get Started with PHP Swoole now!