Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 25.x
Latest version:
pecl install openswoole-25.2.0
Manage a pool of Linux worker processes with Swoole. It can be used to manage a group of worker processes to process messages in MQ like Redis or sent from client side. Native Linux commands can also be executed by the pool of processes.
When a process in the pool is terminated, the pool starts a replacement process automatically.
Swoole\Process\Pool::__constructSwoole\Process\Pool->setSwoole\Process\Pool->onSwoole\Process\Pool->getProcessSwoole\Process\Pool->listenSwoole\Process\Pool->startSwoole\Process\Pool->shutdownSwoole\Process\Pool->writeSwoole\Process\Pool->detachSwoole\Process\Pool->on('start', fn)Swoole\Process\Pool->on('stop', fn)Swoole\Process\Pool->on('workerstart', fn)Swoole\Process\Pool->on('workerstop', fn)Swoole\Process\Pool->on('message', fn)<?php
$workerNum = 10;
$pool = new Swoole\Process\Pool($workerNum);
$pool->on("WorkerStart", function ($pool, $workerId) {
echo "Worker#{$workerId} is started\n";
$redis = new Redis();
$redis->pconnect('127.0.0.1', 6379);
$key = "key1";
while (true) {
$msgs = $redis->brpop($key, 2);
if ( $msgs == null) continue;
var_dump($msgs);
}
});
$pool->on("WorkerStop", function ($pool, $workerId) {
echo "Worker#{$workerId} is stopped\n";
});
$pool->start();