Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Process\Pool->getProcess ( int $worker_id = 0 )
The worker ID
OpenSwoole\Process
Get the process by $worker_id
. By default, it return the current process.
<?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();