Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the concept of swoole cooperation process

Shulou Source: shulou.com Published: 2022-06-01 18:38:45 11月08日 Update

This article introduces the relevant knowledge of "what is the concept of swoole coordination process". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Swoole coroutines are more lightweight threads. A thread can have multiple coroutines. Coroutines are executed inside threads. They are mainly used to convert competing resources in threads into cooperative operations. They are suitable for processing IO-intensive tasks.

Operating environment of this tutorial: Windows 10 system, Swoole version 4, DELL G3 computer

What does SWOOL mean?

First of all, let's talk about the coordination process and understand what it is used for and what its characteristics are.

Coroutines are a lighter entity than threads, and just as a process can have multiple threads, a thread can have multiple coroutines. The coroutine has the following characteristics

User-mode execution, controlled entirely by the program, not managed by the operating system kernel.

It is suitable for processing IO-intensive tasks. As for what IO-intensive tasks are, they are not described in detail here. They are mainly different from CPU-intensive tasks.

Turn competing resources in threads into collaborative operations

Channel communication between protocols

A small amount of context switch overhead, mainly running on threads, compared to the process context switch is stored in the stack resources, while the coroutine is asynchronous non-blocking, equivalent to queue tasks in user-mode threads, only need to use channel as a callback, do not need to grab resources twice after the task is completed

The coroutine can be simply understood as a thread, but this thread is user-state, does not require the participation of the operating system, the cost of creating destruction and switching is very low, and the difference between the coroutine and the thread is that the coroutine cannot utilize the multi-core CPU, and the multi-core CPU needs to rely on Swoole's multi-process model.

Simply put, coroutines are co-operative lightweight threads implemented by programmers hosted under threads and concurrent with lighter ones.

With the increase in the number of programmers, the tycoon also continued explosive growth, of course, some people began to feel that the thread is not easy to use, then what to do? Of course, it is based on the concept of threads to implement a set of lightweight threads that are lighter and easier to deceive stars (in fact, coroutines cannot be completely considered threads, because a thread can have multiple coroutines)

Difference between coroutines and threads

essence

thread kernel state

coroutine user state

scheduling manner

Threads are scheduled in a systematic way, and time-sharing scheduling and preemptive scheduling are common scheduling strategies. To put it bluntly, the scheduling of threads is completely out of their control

The scheduling mode of coroutine is cooperative scheduling, which is not controlled by kernel and switched by free policy scheduling.

"What is the concept of swoole coordination" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Threads scheduling tasks lightweight multiple intensive ways users systems resources switching collaboration kernel programs processes control running concepts operating systems up and down Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Shulou Technology Xiaomi Apple