onWorkerStart

Description:

callback Worker::$onWorkerStart

Sets the callback function that is executed when a Worker child process starts; it will run for each child process when it is started.

Note: onWorkerStart runs when the child process starts. If multiple child processes are started ($worker->count > 1), it will run once for each child process, resulting in a total of $worker->count executions.

Parameters of the Callback Function

$worker

The Worker object

Example

<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onWorkerStart = function(Worker $worker)
{
    echo "Worker {$worker->id} starting...\n";
};
// Run the worker
Worker::runAll();

Tip
Business logic can be distinguished based on worker->id to execute different operations for different processes. For example, specific tasks can be executed only in process 0. See here for details.

Tip
Besides using an anonymous function as a callback, other callback writing methods can also be found here.