OpenSwoole\Process\Pool->getProcess

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

Declaration

<?php OpenSwoole\Process\Pool->getProcess ( int $worker_id = 0 )

Parameters

worker_id

The worker ID

Return

Process

OpenSwoole\Process

Description

Get the process by $worker_id. By default, it return the current process.

Example

<?php
$workerNum = 10;
$pool = new OpenSwoole\Process\Pool($workerNum);
$counter = new OpenSwoole\Atomic();

$pool->on("WorkerStart", function ($pool, $workerId) use ($counter, $workerNum) {
    if ($counter->get() <= $workerNum) {
        $counter->add(1);
        $process = $pool->getProcess();
        $process->exec("/usr/bin/php", ["-r", "echo time() . PHP_EOL;"]);
        echo 'Cannot be executed here', PHP_EOL;
    } else {
        echo 'Finished executing', PHP_EOL;
    }
});

$pool->on("WorkerStop", function ($pool, $workerId) use ($counter, $workerNum) {
    echo 'Worker stop', PHP_EOL;
    if ($counter->get() > $workerNum) {
        $pool->shutdown();
    }
});

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