OpenSwoole\Process\Pool->on('message', fn)

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

Declaration

<?php OpenSwoole\Process\Pool->on('start', callback $callback)

Parameters

event

The event name

callback

callback function

Return

success

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

Description

Executue the callback function when receiving messages.

Example

<?php

$workerNum = 10;
$pool = new OpenSwoole\Process\Pool($workerNum);

$pool->on("WorkerStart", function (OpenSwoole\Process\Pool $pool, int $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 (OpenSwoole\Process\Pool $pool, int $workerId) {
    echo "Worker#{$workerId} is stopped\n";
});

$pool->on('message', function(OpenSwoole\Process\Pool $pool, string $data) {
    echo "$data\n";
});

$pool->start();
Last updated on September 1, 2022