Swoole\Server->on('workerstart', fn)

Declaration

<?php
Swoole\Server->on('workerstart', callback $callback)

Parameters

event

The event name

callback

callback function

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Executue the callback function when Worker Process in the Server is started.

You can check the type of Worker Process based on the value of $worker_id, if $worker_id >= $server->setting['worker_num'], the worker is a Task Worker Process otherwise it is a Worker Process processing requests of the server.

There is no relationship between worker_id and pid of process.

Callback function

<?php
function (Swoole\Server $server, int $worker_id) {

}

Example

Modify the process name when a new worker process starts.

<?php
$serv->on('WorkerStart', function ($serv, $worker_id){
    global $argv;
    if($worker_id >= $serv->setting['worker_num']) {
        swoole_set_process_name("php {$argv[0]} task worker");
    } else {
        swoole_set_process_name("php {$argv[0]} event worker");
    }
});

Get the loaded file list.

<?php
$server = new Swoole\Server("127.0.0.1", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);
$server->on('WorkerStart', function($serv, $workerId) {
    var_dump(get_included_files());
});