Swoole\Process\Pool->detach

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\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 Swoole\Process;
use Swoole\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 August 31, 2022