首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Swoole 入門:重新定義 PHP 的高性能異步網絡通信引擎

     編輯:PHP 相關知識

Swoole 初識:PHP 異步、並行、高性能網絡通信引擎

Swoole 並非傳統 MVC 框架,而是一個用純 C 語言編寫的 PHP 擴展,提供了**異步多線程 Server、異步 TCP/UDP Client、異步 MySQL/Redis、連接池、Task、毫秒定時器、異步文件讀寫、異步 DNS** 等能力。內置 Http/WebSocket/Http2 Server/Client。適用於即時通訊、游戲、物聯網、微服務等高併發場景。

官方定位

Swoole:重新定義 PHP —— PHP 的異步、並行、高性能網絡通信引擎。

核心特性

  • 異步非阻塞 I/O:基於 epoll/kqueue,單進程處理萬級併發。
  • 多進程/線程模型:Master-Manager-Worker-Task 進程結構,利用多核 CPU。
  • 豐富協程支持:Swoole 2.0+ 引入協程,同步寫法實現異步效能。
  • 內置常用 Server/Client:TCP、UDP、HTTP、WebSocket、HTTP2、Redis、MySQL。
  • Task/Finish 機制:耗時任務投遞給 Task Worker,不阻塞 Worker。
  • 毫秒定時器:精準定時任務,替代 crontab。

快速上手:TCP Echo Server & Client

安裝擴展:pecl install swoole(參考 官方 Wiki)。

Server 端 (server.php)

<?php
class Server {
    private $serv;
    public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set([
            'worker_num' => 8,
            'daemonize' => false,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode' => 1
        ]);
        $this->serv->on('Start', [$this, 'onStart']);
        $this->serv->on('Connect', [$this, 'onConnect']);
        $this->serv->on('Receive', [$this, 'onReceive']);
        $this->serv->on('Close', [$this, 'onClose']);
        $this->serv->start();
    }
    public function onStart($serv) { echo "Start\n"; }
    public function onConnect($serv, $fd, $from_id) {
        $serv->send($fd, "Hello {$fd}!");
    }
    public function onReceive(swoole_server $serv, $fd, $from_id, $data) {
        echo "Get Message From Client {$fd}:{$data}\n";
        $serv->send($fd, "Server: {$data}");
    }
    public function onClose($serv, $fd, $from_id) {
        echo "Client {$fd} close connection\n";
    }
}
$server = new Server();

Client 端 (client.php)

<?php
class Client {
    private $client;
    public function __construct() {
        $this->client = new swoole_client(SWOOLE_SOCK_TCP);
    }
    public function connect() {
        if (!$this->client->connect("127.0.0.1", 9501, 1)) {
            echo "Error: {$this->client->errMsg}[{$this->client->errCode}]\n";
            return;
        }
        $message = $this->client->recv();
        echo "Get Message From Server: {$message}\n";
        fwrite(STDOUT, "請輸入消息:");
        $msg = trim(fgets(STDIN));
        $this->client->send($msg);
    }
}
$client = new Client();
$client->connect();

運行測試

# 終端 1
php server.php
# 終端 2
php client.php
# 輸入文字回車,觀察雙向通訊

進階功能一覽

模塊核心功能
Swoole\ServerTCP/UDP/HTTP/WebSocket Server,多進程、事件驅動、Task 投遞
Swoole\Client同步/異步 TCP/UDP/HTTP/WebSocket Client
Swoole\Coroutine協程調度器,提供 go、chan、defer、select 等 CSP 模型
Swoole\Async異步文件讀寫、DNS 查詢、MySQL/Redis/PostgreSQL/HTTP Client
Swoole\Process進程管理、進程間通訊 (IPC)、信號處理
Swoole\Timer毫秒級定時器、定時任務
Swoole\Lock互斥鎖、讀寫鎖、信號量
Swoole\Atomic原子計數器

應用場景

  • 即時聊天、推送服務 (WebSocket)
  • API 网关、微服務節點 (HTTP Server)
  • 游戲服務端、物聯網數據接入 (TCP/UDP)
  • 異步任務隊列、定時任務 (Task + Timer)
  • 高併發爬蟲、數據同步 (異步 Client + 協程)

學習資源

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved