How it works

Swoole is different from the traditional PHP model, it is running in CLI mode.

How Swoole works

Swoole internal

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

  • Swoole supports TCP, UDP and UnixSocket
  • Swoole uses Non-blocking I/O mode with epoll or kqueue
  • Swoole forks several worker processes based on CPU core number to utilise all CPU cores.
  • Swoole supports Long-live connections for WebSocket server or TCP/UDP server.
  • Swoole supports more server-side protocols: TCP/UDP/HTTP/HTTP2/WebSocket
  • Swoole preload PHP files into memory.
  • Swoole can manage and reuse the status in memory with 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 Swoole? Get Started with Swoole now!