Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Server->getWorkerPid(int $workerId = -1): int|bool
workerId (optional)
Return the current main worker process ID.
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
<?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();