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->detach()
Detach current process from process pool manager. The worker process created and managed by the process pool will be isolated from the process pool after $pool->detach()
.
Since v4.7.0
use OpenSwoole\Process;
use OpenSwoole\Coroutine;
$pool = new Process\Pool(2);
$pool->set(['enable_coroutine' => true]);
$pool->on('WorkerStart', function(Process\Pool $pool, $workerId)
{
static $running = true;
Process::signal(SIGTERM, function() use (&$running) {
$running = false;
echo "TERM\n";
});
echo("[Worker #{$workerId}] WorkerStart, pid: " . posix_getpid() . "\n");
$i = 0;
while($running)
{
Coroutine::sleep(1);
$i++;
if($i == 5)
{
$pool->detach();
}
else if($i == 10)
{
break;
}
}
});
$pool->on('WorkerStop', function(Process\Pool $pool, $workerId)
{
echo("[Worker #{$workerId}] WorkerStop, pid: " . posix_getpid() . "\n");
});
$pool->start();