Swoole\Server is an event-driven model, and all business logic code must be written in the event callback function. When a specific network event occurs, the bottom layer will actively call back the specified PHP function.A total of 13 types of events are supported. (Recommended learning:swoole video tutorial)
Event execution sequence
All event callbacks occur after $server->start
The last event when the server shutdown program terminates is onShutdown
After the server is started successfully, onStart/onManagerStart/onWorkerStart will be executed concurrently in different processes
onReceive/onConnect/onClose Triggered in the Worker process
OnWorkerStart/onWorkerStop will be called once when the Worker/Task process starts/ends
The onTask event only occurs in the task process
The onFinish event only occurs in the worker The execution order of the 3 events
onStart/onManagerStart/onWorkerStart occurs in the process is uncertain
Coroutine mode
Swoole2/4 version supports coroutines. After using coroutines, event callback functions will be executed concurrently. Coroutine is a user-mode thread implementation that has no additional scheduling consumption and only occupies memory.
Using the coroutine mode can be understood as "each event callback function will create a new thread for execution. After the event callback function is executed, the thread exits."
If you want to close the coroutine, you can set:
$server->set(["enable_coroutine" => false, ]);
The above is the detailed content of How to use swoole event. For more information, please follow other related articles on the PHP Chinese website!