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
<?php int Swoole\Process\Pool->listen(string $host, int $port = 0, int $backlog = 2048)
host name
port number
TCP backlog size
if success, it returns TRUE, otherwise it returns FALSE.
Bind the process pool with a TCP socket or UDP/Unix socket.
<?php
$pool->listen('127.0.0.1', 8089);
$pool->listen('unix:/tmp/php.sock');
<?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->listen('127.0.0.1', 8089);
$pool->start();