Swoole\Process\Pool->getProcess

Declaration

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

Parameters

worker_id

The worker ID

Return

Process

Swoole\Process

Description

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

Example

<?php
$workerNum = 10;
$pool = new Swoole\Process\Pool($workerNum);
$counter = new Swoole\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();