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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
