Server Event Execution Sequence

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

After looking at all the different Swoole Server Events and how they react under certain conditions, to fully understand how the Swoole server works, it is good to familiarise yourself with the event execution sequence.

The first thing to know is that when you are setting up each event using $server->on they are only executed after you start the server, at this stage you are just registering your event callbacks.

The final event that will be executed is Shutdown if the server has been turned off normally and not forced to stop.

Once you call $server->start the Swoole server will execute Start, ManagerStart and WorkerStart concurrently, so they may complete at the same time or in different orders, in different processes as well.

For a TCP server, the execution sequence for a request is in the order of Connect, Receive and Close which is from the worker process.

A request worker and task worker startup and end events are only called once: WorkerStart and WorkerStop, they both share the same worker thread logic but they operate in their own process space, that is why a task worker is referred to as a worker thread still, they just handle different data from different processes that run under the same server.

The Task event callback is executed within the task process that was selected to execute the task.

When a request worker process sends data to a task worker process, the Finish event is executed within that worker once the task returns any data.

Finally, remember that the events Start, ManagerStart and WorkerStart all execute concurrently, so order of execution cannot be known.

Last updated on March 31, 2022