Baike.dev
All toolsTrendingOpen sourceNewsSubmit
Log in
< 返回工具列表
S

swoole-src

> 编程语言
开源

🚀 Coroutine-based concurrency library for PHP

18.9K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

🚀 Coroutine-based concurrency library for PHP


Swoole is an event-driven, asynchronous, coroutine-based concurrency library with high performance for PHP.

## ⚙️ Quick Start Run Swoole program by [Docker](https://github.com/swoole/docker-swoole) ```bash docker run --rm phpswoole/swoole "php --ri swoole" ``` > For details on how to use it, see: [How to Use This Image](https://github.com/swoole/docker-swoole#how-to-use-this-image). ## Documentation ### HTTP Service ```php $http = new Swoole\Http\Server('127.0.0.1', 9501); $http->set(['hook_flags' => SWOOLE_HOOK_ALL]); $http->on('request', function ($request, $response) { $result = []; Co::join([ go(function () use (&$result) { $result['google'] = file_get_contents("https://www.google.com/"); }), go(function () use (&$result) { $result['taobao'] = file_get_contents("https://www.taobao.com/"); }) ]); $response->end(json_encode($result)); }); $http->start(); ``` ### Concurrency ``` … ``` ## Runtime Hook **Swoole hooks the blocking io function of PHP at the `bottom layer` and `automatically` converts it to a non-blocking function, so that these functions can be called concurrently in coroutines.** ### Supported extension/functions * `ext-curl` (Support `symfony` and `guzzle`) * `ext-redis` * `ext-mysqli` * `ext-pdo_mysql` * `ext-pdo_pgsql` * `ext-pdo_sqlite` * `ext-pdo_oracle` * `ext-pdo_odbc` * `stream functions` (e.g. `stream_socket_client`/`stream_socket_server`), Supports `TCP`/`UDP`/`UDG`/`Unix`/`SSL/TLS`/`FileSystem API`/`Pipe` * `ext-sockets` * `ext-soap` * `sleep`/`usleep`/`time_sleep_until` * `proc_open` * `gethostbyname`/`shell_exec`/`exec` * `fread`/`fopen`/`fsockopen`/`fwrite`/`flock` ## 🛠 Develop & Discussion + __IDE Helper & API__: + __Twitter__: + __Discord__: + __中文社区__: ## 💎 Awesome Swoole Project [Awesome Swoole](https://github.com/swoole/awesome-swoole) maintains a curated list of awesome things related to Swoole, including * Swoole-based frameworks and libraries. * Packages to integrate Swoole with popular PHP frameworks, including Laravel, Symfony, Slim, and Yii. * Books, videos, and other learning materials about Swoole. * Debugging, profiling, and testing tools for developing Swoole-based applications. * Coroutine-friendly packages and libraries. * Other Swoole related projects and resources. ## ✨ Event-based The network layer in Swoole is event-based and takes full advantage of the underlying epoll/kqueue implementation, making it really easy to serve millions of requests. Swoole 4.x uses a brand new engine kernel and now it has a full-time developer team, so we are entering an unprecedented period in PHP history which offers a unique possibility for rapid evolution in performance. ## ⚡ Coroutine Swoole 4.x or later supports the built-in coroutine with high availability, and you can use fully synchronized code to implement asynchronous performance. PHP code without any additional keywords, the underlying automatic coroutine-scheduling. Developers can understand coroutines as ultra-lightweight threads, and you can easily create thousands of coroutines in a single process. ### MySQL Concurrency 10K requests to read data from MySQL takes only 0.2s! ``` … ``` ### Mixed server You can create multiple services on the single event loop: TCP, HTTP, Websocket and HTTP2, and easily handle thousands of requests. ```php function tcp_pack(string $data): string { return pack('N', strlen($data)) . $data; } function tcp_unpack(string $data): string { return substr($data, 4, unpack('N', substr($data, 0, 4))[1]); } $tcp_options = [ 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4 ]; ``` ``` … ``` ### Coroutine clients Whether you DNS query or send requests or receive responses, all of these are scheduled by coroutine automatically. ``` … ``` ### Channel Channel is the only way for exchanging data between coroutines, the development combination of the `Coroutine + Channel` is the famous CSP programming model. In Swoole development, Channel is usually used for implementing connection pool or scheduling coroutine concurrent. #### The simplest example of a connection pool In the following example, we have a thousand concurrently requests to redis. Normally, this has exceeded the maximum number of Redis connections setting and will throw a connection exception, but the connection pool based on Channel can perfectly schedule requests. We don't have to worry about connection overload. ``` … ``` #### Producer and consumers Some Swoole's clients implement the defer mode for concurrency, but you can still implement it flexible with a combination of coroutines and channels. ``` … ``` ### Timer ```php $id = Swoole\Timer::tick(100, function () { echo "⚙️ Do something...\n"; }); Swoole\Timer::after(500, function () use ($id) { Swoole\Timer::clear($id); echo "⏰ Done\n"; }); Swoole\Timer::after(1000, function () use ($id) { if (!Swoole\Timer::exists($id)) { echo "✅ All right!\n"; } }); ``` #### The way of coroutine ```php go(function () { $i = 0; while (true) { Co::sleep(0.1); echo "📝 Do something...\n"; if (++$i === 5) { echo "🛎 Done\n"; break; } } echo "🎉 All right!\n"; }); ``` ## 🔥 Amazing runtime hooks **As of Swoole v4.1.0, we added the ability to transform synchronous PHP network libraries into co-routine libraries using a single line of code.** Simply call the `Swoole\Runtime::enableCoroutine()` method at the top of your script. In the sample below we connect to php-redis and concurrently read 10k requests in 0.1s: ```php Swoole\Runtime::enableCoroutine(); $s = microtime(true); Co\run(function() { for ($c = 100; $c--;) { go(function () { ($redis = new Redis)->connect('127.0.0.1', 6379); for ($n = 100; $n--;) { assert($redis->get('awesome') === 'swoole'); } }); } }); echo 'use ' . (microtime(true) - $s) . ' s'; ``` By calling this method, the Swoole kernel replaces ZendVM stream function pointers. If you use `php_stream` based extensions, all socket operations can be dynamically converted to be asynchronous IO scheduled by coroutine at runtime! ### How many things you can do in 1s? Sleep 10K times, read, write, check and delete files 10K times, use PDO and MySQLi to communicate with the database 10K times, create a TCP server and multiple clients to communicate with each other 10K times, create a UDP server and multiple clients to communicate with each other 10K times... Everything works well in one process! Just see what the Swoole brings, just imagine... ``` … ``` ## ⌛️ Installation > As with any open source project, Swoole always provides the most reliable stability and the most powerful features in **the latest released version**. Please ensure as much as possible that you are using the latest version. ### Compiling requirements + Linux, OS X or Cygwin, WSL + PHP 8.2.0 or later (The higher the version, the better the performance.) + GCC 4.8 or later ### 1. Install via PECL (beginners) ```shell pecl install swoole ``` ### 2. Install from source (recommended) Please download the source packages from [Releases](https://github.com/swoole/swoole-src/releases) or clone a specific version. Don't use `master` branch as it may be in development. To clone the source code from git specify a tag: ```shell git clone --branch v6.0.0 --single-branch https://github.com/swoole/swoole-src.git && \ cd swoole-src ``` Compile and install at the source folder: ```shell phpize && \ ./configure && \ make && make install ``` #### Enable extension in PHP After compiling and installing to the system successfully, you have to add a new line `extension=swoole.so` to `php.ini` to enable Swoole extension. #### Extra compiler configurations > for example: `./configure --enable-openssl --enable-sockets` + `--enable-openssl` or `--with-openssl-dir=DIR` + `--enable-sockets` + `--enable-mysqlnd` (need mysqlnd, it just for supporting `$mysql->escape` method) + `--enable-swoole-curl` ### Upgrade > ⚠️ If you upgrade from source, don't forget to `make clean` before you upgrade your swoole 1. `pecl upgrade swoole` 2. `cd swoole-src && git pull && make clean && make && sudo make install` 3. if you change your PHP version, please re-run `phpize clean && phpize` then try to compile ### Major change since version 4.3.0 Async clients and API are moved to a separate PHP extension `swoole_async` since version 4.3.0, install `swoole_async`: ```shell git clone https://github.com/swoole/ext-async.git cd ext-async phpize ./configure make -j 4 sudo make install ``` Enable it by adding a new line `extension=swoole_async.so` to `php.ini`. ## 🍭 Benchmark + On the open source [Techempower Web Framework benchmarks](https://www.techempower.com/benchmarks/#section=data-r17) Swoole used MySQL database benchmark to rank first, and all performance tests ranked in the first echelon. + You can just run [Benchmark Script](https://github.com/swoole/benchmark/blob/master/benchmark.php) to quickly test the maximum QPS of Swoole-HTTP-Server on your machine. ## 🔰️ Security issues Security issues should be reported privately, via email, to the Swoole develop team [[email protected]](mailto:[email protected]). You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. ## 🖊️ Contribution Your contribution to Swoole development is very welcome! You may contribute in the following ways: * [Report issues and feedback](https://github.com/swoole/swoole-src/issues) * Submit fixes, features via Pull Request * Write/polish documentation ## ❤️ Contributors This project exists thanks to all the people who contribute. [[Contributors](https://github.com/swoole/swoole-src/graphs/contributors)]. ## 🎙️ Official Evangelist [Demin](https://deminy.in) has been playing with PHP since 2000, focusing on building high-performance, secure web services. He is an occasional conference speaker on PHP and Swoole, and has been working for companies in the states like eBay, Visa and Glu Mobile for years. You may find Demin on [Twitter](https://twitter.com/deminy) or [GitHub](https://github.com/deminy). ## 📃 License Apache License Version 2.0 see http://www.apache.org/licenses/LICENSE-2.0.html

核心特点

  • •ext-curl (Support symfony and guzzle)
  • •ext-redis
  • •ext-mysqli
  • •ext-pdo_mysql
  • •ext-pdo_pgsql
  • •ext-pdo_sqlite
  • •ext-pdo_oracle
  • •ext-pdo_odbc
  • •stream functions (e.g. stream_socket_client/stream_socket_server), Supports TCP/UDP/UDG/Unix/SSL/TLS/FileSystem API/Pipe
  • •ext-sockets

> 标签

C++concurrencycoroutineseventphp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月9日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言
Baike.dev

baike.dev helps you discover great languages, frameworks, databases, DevOps and cloud-native tools.

Quick links

  • Home
  • All tools
  • Trending
  • Open source

About

  • About us
  • Community
  • News

Contribute

Found a great developer tool? Share it with the community.

Submit a tool
© 2026 baike.dev Developer EncyclopediaUpdated daily · Discover great developer tools