May 14, 2021 Swoole
In swoole, a swoole_server has several callback functions that determine the relevant functions swoole_server the system. Can be done
$serv->on( string $event, mixed $callback );
function to configure. Example:
class Server
{
private $serv;
public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));
$this->serv->start();
}
}
All callback functions and related descriptions are as follows:
[TOC]
Description: A callback that receives data
Function prototype:
function onReceive( swoole_server $serv, $fd, $from_id, $data );
Parameters | Describe |
---|---|
$serv | swoole_server object |
$fd | The descriptor of the connection |
$from_id | reactor's id, useless |
$data | The data received |
Description: Whenever the server receives data from the client, the data is delivered to the Worker via the onReceive callback. I f protocol detection is turned on, callbacks are not responded to until the full packet is received. Note that the callback function must be set or the server cannot be started.
Description: A callback initiated by the server
Function prototype:
function onStart( swoole_server $serv);
Parameters | Describe |
---|---|
$serv | swoole_server object |
Description:
The onStart event is called in the main thread of the Master process.
Before this callback response, Swoole Server had done the following
Next, do it
In the onStart callback, only echo, print Log, and modify process names are allowed. N
o other action should be taken. T
he onWorkerStart and onStart callbacks are executed in parallel in different processes, without sequentiality. Y
ou can save the values
of $serv-gt;master_pid and
$serv-gt;manager_pid
in the onStart callback to a file.
This allows you to script and send signals to both PIDs for shutdown and restart operations.
The timer is no longer supported from the 1.7.5 plus Master process, and the onMasterConnect/onMasterClose 2 event callbacks are completely removed.
No PHP interfaces are retained within the Master process.
Global resource objects created in onStart cannot be used in the worker process because the worker process was created by the time the onStart call occurred.
Newly created objects are in the main process, and the worker process cannot access this memory area, so the code created by the global object needs to be placed swoole_server_start the database.
Description: A callback that the Worker process starts
Function prototype:
function onWorkerStart( swoole_server $serv,int $worker_id);
Parameters | Describe |
---|---|
$serv | swoole_server object |
$worker_id | The id of the Worker process |
Description: This event occurs when the worker process/task_worker process starts.
In the event of a PHP fatal error or an active call to exit in code, the Worker/Task process exits and the management process re-creates the new process onWorkerStart/onStart, which is executed in unordered order
Using the worker_id of the $300 parameter, determine whether the worker is a normal worker task_worker.
$worker said the process was $serv when the worker_num was "task_worker".
If you want swoole_server_reload implement code overloading, you must require your business file in WorkStart, not at the head of the file.
Files that were included before the onWorkerStart call are not reloaded with code.
You can place common, irhyable php files before the onWorkerStart.
This way, although the code cannot be overloaded, all workers are shared and do not require additional memory to hold the data.
The code after onWorkerStart each worker needs to keep a copy in memory of $worker-id is a number from 0-$worker-num, indicating that the ID $worker-id of the worker process has nothing to do with the process PID
Description: A callback when a new connection is connected
Function prototype:
function onConnect( swoole_server $serv,int $fd, int $from_id);
Parameters | Describe |
---|---|
$serv | swoole_server object |
$fd | The descriptor of the connection |
$from_id | reactor's id, useless |
Description: When a new connection enters, the callback is made in the worker process. T he two callbacks onConnect/onClose occur within the worker process, not the main process. I f you need to handle connection/close events in the main process, register the onMasterConnect/onMasterClose callback. The onMasterConnect/onMasterClose callback is always executed before the onConnect/onClose
Description: A callback when the connection is closed
Function prototype:
function onClose( swoole_server $serv,int $fd, int $from_id);
Parameters | Describe |
---|---|
$serv | swoole_server object |
$fd | The descriptor of the connection |
$from_id | reactor's id, useless |
Description: After the TCP client connection is closed, this function is called back in the worker process. T his event is triggered whether the close is initiated by the client or swoole_server_close server-side active call to close the connection. So as long as the connection is closed, you'll definitely call back to this function.
Description: A callback task_worker process processing tasks in the process
Function prototype:
function onTask(swoole_server $serv, int $task_id, int $from_id, string $data);
Parameters | Describe |
---|---|
$serv | swoole_server object |
$task_id | The task ID |
$from_id | From which worker process |
$data | The content of the task |
Description: Called task_worker process within the process. T he worker process can swoole_server_task a function to task_worker new tasks to the worker process. T he task result string can be returned directly to the worker process via return. T he worker process will receive the result in the onFinish callback. Note: If the serv-gt;set ('task_worker_num') is not incremental task_id from 1-8.
Description: This task_worker callback to the end of the process processing task
Function prototype:
function onFinish(swoole_server $serv, int $task_id, string $data);
Parameters | Describe |
---|---|
$serv | swoole_server object |
$task_id | The task ID |
$data | The result of the task |
Description: In this function, you receive the results of task processing to distinguish task_id tasks worker_id different tasks by using the data.
Description: A callback triggered by a timer
Function prototype:
function onTimer(swoole_server $serv, int $interval);
Parameters | Describe |
---|---|
$serv | swoole_server object |
$interval | Timed intervals |
Description: When the timer is triggered, the function is called. Interval is used to distinguish between timers at different intervals.