What is the realization principle of swoole synergetic process?

01-27-2024

In swoole, the data received by Swoole server triggers the onReceive callback in the worker process, resulting in a coroutine. Swoole creates a corresponding Ctrip for each request, and a sub-coroutine can also be created in the coroutine. The coroutine is single-threaded in the underlying implementation, so only one coroutine is working at the same time.

Operating environment of this course: Windows S10 system, Swoole4 version 4 and DELL G3 computer.

What is the principle of swoole process? What is a process?

A process is a startup instance of an application. Independent file resources, data resources and memory space.

What is a thread?

A thread belongs to a process and is the executor of a program. A process contains at least one main thread, and there may be more sub-threads. Threads have two scheduling strategies, one is time-sharing scheduling, and the other is preemptive scheduling.

What is xiecheng?

Coagulation is a lightweight thread, and it belongs to a thread, and it is executed in a thread. The scheduling of the process is manually switched by the user, so it is also called user space thread. The creation, switching, suspension and destruction of the process are all memory operations, and the consumption is very low. The scheduling strategy of the collaborative process is: cooperative scheduling.

The principle of Swoole concordance

Swoole4 is single-threaded and multi-process, and only one process will be running at the same time.

Swoole server receives data and triggers onReceive callback in worker process to generate a Ctrip. Swoole creates a corresponding Ctrip for each request. You can also create sub-coroutines in coroutines.

The process is single-threaded in the underlying implementation, so only one process is working at the same time, and the execution of the process is serial.

Therefore, when multi-task and multi-process are executed, while one process is running, other processes will stop working. The current process will hang when it performs blocking IO operation, and the underlying scheduler will enter the event loop. When there is an IO completion event, the underlying scheduler resumes the execution of the coroutine corresponding to the event. . Therefore, there is no IO time-consuming process, which is very suitable for high concurrent IO scenarios. (as shown below)

Swoole's synergetic execution process

There is no IO waiting for the normal execution of PHP code, and there will be no execution process switching.

When the coroutine encounters IO waiting, it immediately cuts the control right, and after IO is completed, it cuts the execution flow back to the point cut out by the coroutine.

Concurrent coordination is executed in turn, with the same logic as above.

The nested execution flow of the coroutine enters from the outside to the inside layer by layer until IO occurs, and then cuts to the outer coroutine. The parent coroutine will not wait for the end of the child coroutine.

Execution sequence of the coordination process

Let's take a look at the basic example first:

go(function () { echo "hello go1 \n"; }); echo "hello main \n"; go(function () { echo "hello go2 \n"; });

Go () is the abbreviation of \Co::create (), which is used to create a coroutine and takes callback as a parameter. The code in callback will be executed in this new coroutine.

Remarks: \Swoole\Coroutine can be abbreviated as \ co.

The above code execution results:

root@b98940b00a9b /v/w/c/p/swoole# php co.phphello go1 hello main hello go2

There seems to be no difference between the execution results and the order in which we usually write the code. The actual execution process:

Run this code and the system starts a new process.

When go () is encountered, a coroutine is generated in the current process, heelo go1 is output in the coroutine, and the coroutine exits.

The process continues to execute the code downwards, outputting hello main.

Generate another coroutine, output heelo go2 in coroutine, and exit coroutine.

Run this code to start a new process. If you don't understand this sentence, you can use the following code:

// co.php

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us