Swoole Server getWorkerPid()

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Server->getWorkerPid(int $workerId = -1): int|bool

Parameters

workerId

workerId (optional)

Return

workerPid

Return the current main worker process ID.

Description

Get the worker process Id based on worker Id. If no worker Id is provided, return the process Id of current worker processing the request.

Available since > v4.5.0

Example:

<?php
$serv = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS);
$serv->set(array(
    'log_level' => SWOOLE_LOG_ERROR,
    'worker_num' => 2,
));
$serv->on("WorkerStart", function (Swoole\Server $serv, $workerId) use ($pm) {
    $GLOBALS['pid_worker_'.$workerId] = posix_getpid();
});
$serv->on('Request', function ($req, $resp) use ($serv) {
    $resp->end(json_encode(['result' =>
        $GLOBALS['pid_worker_'.$serv->worker_id] == $serv->getWorkerPid($serv->worker_id) &&
        $serv->getWorkerPid(0) != $serv->getWorkerPid(1)]
    ));
});
$serv->start();
Last updated on August 31, 2022