OpenSwoole\Process\Pool->detach

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

Declaration

<?php OpenSwoole\Process\Pool->detach()

Parameters

Return

Description

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

Example

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();
Last updated on September 1, 2022