In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge of what the implementation process of swoole is, detailed content and clear logic. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
In swoole, Swoole server receives data when the worker process triggers the onReceive callback to generate a co-program. Swoole creates a corresponding Ctrip for each request, and sub-programs can also be created in the co-program. The co-program is single-threaded in the underlying implementation, so only one co-program is working at the same time.
Operating environment of this tutorial: Windows10 system, Swoole4 version, DELL G3 computer
What is the implementation principle of swoole protocol?
What is the process?
A process is a startup instance of an application. Independent file resources, data resources, memory space.
What is a thread?
Threads belong to the process and are the executors of the program. A process contains at least one main thread, or it can have more child threads. There are two scheduling strategies for threads, one is time-sharing scheduling, and the other is preemptive scheduling.
What is a cooperative journey?
The co-program is a lightweight thread, the co-program also belongs to the thread, and the co-program is executed in the thread. The scheduling of the cooperative program is manually switched by the user, so it is also called the user space thread. The creation, switching, suspending and destroying of the co-program are all memory operations, and the consumption is very low. The scheduling strategy of collaborative process is: collaborative scheduling.
The principle of Swoole Co-programming
Because Swoole4 is single-threaded and multi-process, there is only one co-program running in the same process at a time.
Swoole server receives data in the worker process triggers the onReceive callback to generate a Ctrip. Swoole creates a corresponding Ctrip for each request. A sub-cooperative process can also be created during the collaborative process.
The co-program is single-threaded in the underlying implementation, so only one co-program is working at the same time, and the execution of the co-program is serial.
Therefore, when multitasking and multiprogramming is executed, while one is running, the other will stop working. The current protocol hangs when it performs a blocking IO operation, and the underlying scheduler enters the event loop. When an IO completes the event, the underlying scheduler resumes the execution of the corresponding co-program. Therefore, there is no IO time consuming in the collaborative process, which is very suitable for high concurrency IO scenarios.
Cooperative process execution flow of Swoole
There is no IO waiting for the PHP code to be executed normally in the cooperative program, so there is no execution process switch.
When the cooperative process encounters IO, it waits for the control to be cut immediately, and after the IO is completed, the execution stream will be re-cut back to the point where the original collaborative program was cut out.
The parallel program is executed in turn, same as the previous logic.
The nested execution process of the co-program enters layer by layer from the outside to the inside until IO occurs, and then cuts to the outer layer of the co-program. The parent co-program does not wait for the child co-program to finish.
The execution order of the cooperative program
Let's take a look at the basic example:
Go (function () {echo "hello go1\ n";}); echo "hello main\ n"; go (function () {echo "hello go2\ n";})
Go () is an acronym for\ Co::create (), which is used to create a co-program, accept callback as a parameter, and the code in callback will be executed in this new co-program.
Remarks:\ Swoole\ Coroutine can be abbreviated to\ Co
The result of the above code execution:
Root@b98940b00a9b / v/w/c/p/swoole# php co.phphello go1hello mainhello go2
The execution result seems to be no different from the order in which we usually write the code. The actual implementation process:
Run this code and the system starts a new process
When go () is encountered, a co-program is generated in the current process. Heelo go1 is output in the co-program, and the co-program exits.
The process continues to execute the code down, outputting hello main
Regenerate into a co-program in which heelo go2 is output and the co-program exits
Run this code and the system starts a new process. If you don't understand this sentence, you can use the following code:
/ / co.php
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.