Detailed explanation of php coroutine (with examples)

不言
Release: 2023-04-04 08:14:01
Original
5107 people have browsed it

This article brings you a detailed explanation of PHP coroutines (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Multi-tasking (parallel and concurrency)

Before talking about coroutines, let’s talk about multi-processes, multi-threads, parallelism and concurrency.

For single-core processors, the principle of multi-process multitasking is to let the operating system allocate a certain CPU time slice to a task each time, then interrupt, let the next task execute a certain time slice, and then interrupt again And move on to the next one, and so on.

Since switching execution tasks is very fast, the impression given to external users is that multiple tasks are executed simultaneously.

The scheduling of multi-processes is implemented by the operating system. The process itself cannot control when it is scheduled. That is to say: The scheduling of processes is performed by the outer scheduler Preemptive implementation

The coroutine requires the currently running task to automatically pass control back to the scheduler, so that other tasks can continue to run. This is exactly the opposite of preemptive multitasking. The scheduler of preemptive multitasking can forcefully interrupt running tasks regardless of its own wishes. If only the program were to automatically hand over control, it would be easy for some malicious program to take up all of the CPU time without sharing it with other tasks.

The scheduling of the coroutine is implemented by the coroutine itselfactivelygiving up control to the outer scheduler

Back to the example of the generator implementing the xrange function, The alternation of the entire execution process can be represented by the following figure:

Coroutines can be understood as pure user-mode threads, which perform task switching through collaboration rather than preemption .

Compared with processes or threads, all operations of coroutines can be completed in user mode instead of operating system kernel mode, and the cost of creation and switching is very low.

Simply putCoroutine is to provide a method to interrupt the execution of the current task, save the current local variables, and restore the current local variables to continue execution next time.

We can split the large task into multiple small tasks and execute them in turn. If there is a small task waiting for system IO, skip it and execute the next small task. In this way, we can schedule back and forth to achieve IO. The parallel execution of operations and CPU calculations generally improves the execution efficiency of tasks, which is the meaning of coroutines

Multi-threading
Under a single core, multi-threads must be concurrent;
However, the current multi-threading of the unified process can run under multi-core CPU, so it can be parallel

Concurrency (Concurrency)

refers to the ability to handle multiple simultaneous activities , concurrent events do not necessarily have to occur at the same time.

Parallel (Parallesim)

refers to two concurrent events that occur at the same time, which has the meaning of concurrency, but concurrency is not necessarily parallel.
Multiple operations can be performed within overlapping time periods.

The difference between parallelism and concurrency

Concurrency refers to the structure of the program, Parallel refers to the state of the program when it is running
Parallel must be concurrent, Parallel is a type of Concurrency design
A single thread can never reach the Parallel state

Coroutine

Coroutine support is based on generator, adding the function of sending data back to the generator (the caller sends data to the called generator function) .
This changes the one-way communication from the generator to the caller into a two-way communication between the two.
We have already talked about the send method in the previous article, let us understand the coroutine

Synchronous code

Before asynchronous execution code is involved, our code is like this

function printNum($max, $caller)
{
    for ($i=0; $i<$max; $i++ ) {
        echo "调度者:" . $caller . "  打印:" . $i . PHP_EOL;
    }
}

printNum(3, "caller1");
printNum(3, "caller2");

# output
调度者:caller1  打印:0
调度者:caller1  打印:1
调度者:caller1  打印:2
调度者:caller2  打印:0
调度者:caller2  打印:1
调度者:caller2  打印:2
Copy after login

Improved code after using coroutines

First draft, Manually adjust generator execution

# 本代码手动调整了进程执行代码的顺序,当然本代码实现不用协程也可以,只是利用本流程说明协程作用
# 生成器给了我们函数中断,协程[生成器send]给了我们重新唤起生成器函数的能力
function printNumWithGen($max)
{
    for ($i=0; $i<$max; $i++ ) {
        $res = yield $i;
        echo $res;
    }
}

$gen1 = printNumWithGen(3);
$gen2 = printNumWithGen(3);

// 手动执行caller1 再 caller2
$gen1->send("调度者: caller1 打印:" . $gen1->current() . PHP_EOL);
$gen2->send("调度者: caller2 打印:" . $gen2->current() . PHP_EOL);

// 手动执行caller1 再 caller2
$gen1->send("调度者: caller1 打印:" . $gen1->current() . PHP_EOL);
$gen2->send("调度者: caller2 打印:" . $gen2->current() . PHP_EOL);

// 手动执行caller2 再 caller1
$gen2->send("调度者: caller2 打印:" . $gen2->current() . PHP_EOL);
$gen1->send("调度者: caller1 打印:" . $gen1->current() . PHP_EOL);

# output
调度者: caller1 打印:0
调度者: caller2 打印:0
调度者: caller1 打印:1
调度者: caller2 打印:1
调度者: caller2 打印:2
调度者: caller1 打印:2
Copy after login

Summary

The above case should let everyone understand the meaning of coroutine design and how to use coroutines
Then let’s automatically create one for our coroutine Automatic scheduler (Co automatic executor), no need to manually interrupt and resume

The above is the detailed content of Detailed explanation of php coroutine (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template