Process \ pool:: detach() for preview of new features in spool v4.7

Time:2021-9-22

Process\PoolIt is a process pool provided by swoole, based onServerThe manager management process module can manage multiple work processes.

The core function of this module is process managementProcessImplement multiple processes,Process\PoolIt is simpler and has a higher encapsulation level. Developers can realize the process management function without writing too much codeCoroutine\ServerYou can create a pure collaborative style server-side program that can use multi-core CPU.

In version 4.7, theProcess\PoolAdded onedetachMethod, does the method name look familiar?

stayHttp\ResponseThere is also one of themdetachMethod, which separates the response object. After using this method,$responseObjects are not automatically destroyedend, andHttp\Response::createandServer->sendUse together.

Method function

thatProcess\Pool::detach()The role of is obvious:

When the current worker process in the process pool is separated from management, the bottom layer will immediately create a new process, the old process will no longer process data, and the application layer code will manage the life cycle by itself.

Sample code

Let’s look at the sample code:

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();
        } elseif ($i == 10) {
            break;
        }
    }
});
$pool->on('WorkerStop', function (Process\Pool $pool, $workerId) {
    echo("[Worker #{$workerId}] WorkerStop, pid: " . posix_getpid() . "\n");
});
$pool->start();

stayWorkerStartPassed inProcess::signalSet an asynchronous signal monitoring, which can be sent bySIGTERMSignal to stop the service.

When the service is running$iWhen it is equal to 5, the current process is separated from management; At the same time, the bottom layer will create new processes to maintainworker_numquantity When$iEqual to 10, the process ends.

Therefore, you will get the following output:

[Worker #0] WorkerStart, pid: 75050
[Worker #1] WorkerStart, pid: 75051
[Worker #0] WorkerStart, pid: 75054
[Worker #1] WorkerStart, pid: 75055
[Worker #0] WorkerStop, pid: 75050
[Worker #1] WorkerStop, pid: 75051
[Worker #1] WorkerStart, pid: 75056
[Worker #0] WorkerStart, pid: 75057

In the above code, it is equivalent to maintaining four processes. After one exit, two new processes will be pulled up again, such as reciprocating.

When using, you need to pay special attention to logic problems, otherwise it may lead to infinite creation of new processes.

Process \ pool:: detach() for preview of new features in spool v4.7