OpenSwoole\Process\Pool->listen(string $host, int $port = 0, int $backlog = 2048)

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php int OpenSwoole\Process\Pool->listen(string $host, int $port = 0, int $backlog = 2048)

Parameters

host

host name

port

port number

backlog

TCP backlog size

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

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');

Example

<?php

$workerNum = 10;
$pool = new OpenSwoole\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();
Last updated on September 1, 2022