Home  >  Article  >  PHP Framework  >  How to implement swoole coroutine

How to implement swoole coroutine

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-10 16:20:022749browse

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秒

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:
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
Previous article:Is Swoole written in c?Next article:Is Swoole written in c?