Home PHP Framework Swoole How to implement swoole coroutine

How to implement swoole coroutine

Dec 10, 2019 pm 04:20 PM
swoole coroutine accomplish

How to implement swoole coroutine

Swoole4 provides a powerful CSP coroutine programming model for the PHP language. Users can create a coroutine through the go function to achieve concurrent execution, as shown in the following code:

<?php
//Co::sleep()是Swoole提供的API,并不会阻塞当前进程,只会阻塞协程触发协程切换。
go(function (){
    Co::sleep(1);
    echo "a";
});
go(function (){
    Co::sleep(2);
    echo "b";
});
echo "c";
//输出结果:cab
//程序总执行时间2秒
Copy after login

In fact, the multi-coroutine programming mode was implemented before Swoole4. When the coroutine is created, switched and ended, the php stack can be operated accordingly (create, switch and recycle the php stack).

The coroutine implementation at this time cannot perfectly support PHP syntax. The fundamental reason is that the c stack information is not saved. (APIs provided inside the vm or by some extensions are implemented through c functions. If coroutine switching occurs when calling these functions, how should the c stack be handled?)

Swoole4 has added the c stack Management, when the coroutine is created, switched and ended, it will be accompanied by the creation, switching and recycling of the c stack.

Swoole4 coroutine implementation plan is shown in the figure below:

How to implement swoole coroutine

##Among them:

·The API layer provides coroutine-related functions for users. For example, the go() function is used to create coroutines; Co::yield() causes the current coroutine to give up the CPU; Co::resume () can resume the execution of a certain coroutine;

·Swoole4 coroutine needs to manage the c stack and php stack at the same time. Coroutine is used to manage the c stack, and PHPCoroutine is used It is used to manage the php stack; among them, Coroutine(), yield(), and resume() realize the creation and swap-in and swap-out of the c stack; create_func(), on_yield(), on_resume() realize the creation and swap-in of the php stack. Out;

·Swoole4 uses the boost.context library when managing the c stack. The make_fcontext() and jump_fcontext() functions are both written in assembly language and implemented The creation and switching of c stack context;

·Swoole4 simply encapsulates boost.context, namely Context layer, Context(), SwapIn() and SwapOut()

corresponds to the creation and swapping in and out of the c stack.

PHP Chinese website has a large number of free

Swoole introductory tutorials, everyone is welcome to learn!

The above is the detailed content of How to implement swoole coroutine. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones?

The parent-child relationship between golang functions and goroutine The parent-child relationship between golang functions and goroutine Apr 25, 2024 pm 12:57 PM

The parent-child relationship between golang functions and goroutine

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones

Which one is better, swoole or workerman? Which one is better, swoole or workerman? Apr 09, 2024 pm 07:00 PM

Which one is better, swoole or workerman?

How does swoole_process allow users to switch? How does swoole_process allow users to switch? Apr 09, 2024 pm 06:21 PM

How does swoole_process allow users to switch?

How to use swoole coroutine in laravel How to use swoole coroutine in laravel Apr 09, 2024 pm 06:48 PM

How to use swoole coroutine in laravel

Application of concurrency and coroutines in Golang API design Application of concurrency and coroutines in Golang API design May 07, 2024 pm 06:51 PM

Application of concurrency and coroutines in Golang API design

Which one has better performance, swoole or java? Which one has better performance, swoole or java? Apr 09, 2024 pm 07:03 PM

Which one has better performance, swoole or java?

See all articles