Introduction to Swoole

May 14, 2021 17:00 Swoole

Table of contents

Introduction to Swoole

Swoole is an extesion extension running under PHP, which is actually different from a normal extension. A normal extension simply provides a library function. T he swoole extension, on the other hand, takes over control of PHP after it runs and enters the event loop. W hen an IO event occurs, swoole automatically calls back to the specified PHP function.

PHP's asynchronous, parallel, high-performance network communication engine, written in pure C language, provides PHP-language asynchronous multithreaded servers, asynchronous TCP/UDP network clients, asynchronous MySQL, asynchronous Redis, database connection pools, AsyncTask, message queues, millisecond timers, asynchronous file read and write, asynchronous DNS queries. Swoole has built-in Http/WebSocket server/client, Http2.0 server side.

Swoole can be widely used in the Internet, mobile communications, enterprise software, online games, Internet of Things, car networking, smart home and other fields. Using PHP-Swoole as a network communications framework can greatly improve the efficiency of enterprise IT research and development teams and focus more on developing innovative products.

The Swoole underlying has an asynchronous non-blocking, multithreaded network IO server built in. P HP programmers only need to handle event callbacks without having to care about the underlying. Unlike all asynchronous frameworks such .js Nginx / Tornado / Node, Swoole supports both full asynchronous and synchronization.

Swoole is open source free software and the license agreement is Apache 2.0. Both business and individual developers can use Swoole's code for free, and changes made on top of Swoole can be used for commercial products without the need for open source (Note: the original author's copyright notice must be retained).

The implementation of Swoole

Swoole is written in pure C and does not rely on other third-party libraries.

  • Swoole doesn't use libevent, so you don't need to install libevent
  • Swoole does not rely on extensions such as php's stream / sockets / pcntl / posix / sysvmsg

The socket section

Swoole uses the underlying socket system call. See sys/socket.h

The IO event loop

  • The event loop of the main process uses select/poll because there are only a few file descriptors in the main thread, which can be used
  • The reactor thread/worker process uses epoll/kqueue
  • The task process does not have an event loop, and the process loops through the read pipeline

There are a lot of people who use tracke-p to see that the main swoole process can only see the poll system call. The correct way to view this is trace -f -p


  • Multi-processes use fork() system calls
  • Multithreaded use the pthread thread library


Swoole uses eventfd as a mechanism for thread/inter-process message notification.


Swoole uses timerfd to implement the timer


swoole uses signalfd to shield and process signals. Y ou can effectively avoid the problem of threads/processes being interrupted by signals and the system calling restart. The actor thread does not accept any signals in the main process.

1.8.7 or later is fully compatible with PHP7