Scripts2026年5月19日·1 分钟阅读

Swoole — Coroutine-Based Async PHP Runtime

Swoole is a high-performance coroutine-based asynchronous programming framework for PHP that turns PHP into a language capable of handling concurrent connections like Go or Node.js.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Swoole Overview
通用 CLI 安装命令
npx tokrepo install 9dd607d7-53c0-11f1-9bc6-00163e2b0d79

Introduction

Swoole extends PHP with an event-driven, coroutine-based concurrency model. It allows PHP developers to build high-performance TCP/UDP/HTTP/WebSocket servers without switching languages.

What Swoole Does

  • Adds native coroutine support to PHP for concurrent I/O
  • Provides built-in HTTP, WebSocket, and TCP/UDP servers
  • Implements async MySQL, PostgreSQL, and Redis clients
  • Offers process management and task worker pools
  • Supports millisecond-precision timers and signal handling

Architecture Overview

Swoole is a C extension for PHP that runs as a persistent process (not per-request like PHP-FPM). It uses an event loop powered by epoll/kqueue at its core, with a multi-process reactor model. Coroutines are stackful and scheduled cooperatively, yielding automatically on I/O operations so other coroutines can proceed.

Self-Hosting & Configuration

  • Install via PECL (pecl install swoole) or compile from source
  • Enable the extension in php.ini with extension=swoole.so
  • Configure worker count via $server->set(['worker_num' => 4])
  • Use Docker image phpswoole/swoole for containerized deployments
  • Set swoole.use_shortname to control function alias behavior

Key Features

  • Stackful coroutines with automatic yield on I/O
  • Built-in connection pooling for databases and Redis
  • HTTP/2 and WebSocket server support out of the box
  • Shared memory tables for inter-process data sharing
  • Process and task worker management with message passing

Comparison with Similar Tools

  • ReactPHP — event-loop async without coroutines; Swoole uses coroutines for synchronous-style code
  • Workerman — pure PHP async server; Swoole is a C extension with higher raw throughput
  • RoadRunner — Go-based PHP app server; Swoole runs entirely within PHP process space
  • FrankenPHP — embedded PHP in Caddy; Swoole provides its own server and async primitives
  • Node.js — similar event-driven model but Swoole lets teams keep their PHP codebase

FAQ

Q: Does Swoole work with Laravel? A: Yes. Laravel Octane provides a first-party Swoole adapter for persistent application serving.

Q: Can I use existing PHP libraries with Swoole? A: Most libraries work, but blocking I/O calls should be replaced with Swoole coroutine-aware clients for best performance.

Q: Is Swoole production-ready? A: Yes. It has been used in production by companies handling millions of concurrent connections since its initial release in 2012.

Q: What PHP versions does Swoole support? A: Swoole supports PHP 8.1 and above for the latest releases.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产