Detailed explanation of php co process knowledge points

  • 2021-11-01 23:54:48
  • OfStack

Multitasking (parallel and concurrent)

Before talking about synergy, let's talk about multi-process, multi-thread, parallelism and concurrency.

For single-core processors, the principle of multi-process multitasking is to let the operating system assign one set of CPU time slices to one task at a time, then interrupt, let the next task execute one set of time slices, then interrupt and continue to execute the next one, and so on.

Because the speed of switching tasks is very fast, the feeling to external users is that multiple tasks are executed at the same time.

Multi-process scheduling is realized by the operating system, and the process itself cannot control when it is scheduled, that is to say, the scheduling of the process is preemptively realized by the outer scheduler

Scheduling requires that the currently running task automatically pass control back to the scheduler, so that other tasks can continue to run. This is the opposite of preemptive multitasking, where the scheduler can force interruptions of a running task, whether it wants to or not. If you rely only on programs to hand over control automatically, then some malicious programs will easily take up all CPU time without sharing it with other tasks.

Scheduling of the co-process is realized by the co-process itself actively giving up control rights to the outer scheduler

Returning to the example where the generator implemented the xrange function just now, the alternation of the whole execution process can be represented by the following figure:

Co-process can be understood as a pure user-mode thread, which switches tasks through cooperation instead of preemption.

Compared with a process or thread, all operations of a co-process can be completed in user mode instead of kernel mode of the operating system, and the consumption of creation and switching is very low.

Simply speaking, Synergy is to provide a method to interrupt the execution of the current task, save the current local variables, and then resume the current local variables to continue execution next time.

We can split a large task into several small tasks to execute in turn. If there is a small task waiting for the system IO, we will skip it and execute the next small task. This reciprocating scheduling realizes the parallel execution of IO operation and CPU calculation, and generally improves the execution efficiency of tasks, which is the significance of synergy

Multithreading

Under single core, multithreading must be concurrent;

However, the multithreading of the current unified 1 process can run under the multi-core CPU, so it can be parallel

Concurrent (Concurrency)

It refers to the ability to handle multiple simultaneous activities, and concurrent events must occur at the same time.

Parallel (Parallesim)

It refers to two concurrent events occurring at the same time, which has the meaning of concurrency, while concurrency is not definite concurrency.
Multiple operations can be performed in overlapping time periods.

Difference between parallelism and concurrency

Concurrency refers to the structure of a program, and parallelism refers to the state of the program when it is running

Parallelism 1 is concurrent, and parallelism is a kind of concurrent design

Single thread can never achieve parallel state

Synergetic process

The support of Synergetic is based on the generator, adding the function of sending back data to the generator (the caller sends data to the called generator function).

This transforms one-way communication from generator to caller into two-way communication between them.

We talked about the send method in the previous article, so let's understand the next coordination process

Synchronization code

Our code is like this until it involves asynchronous execution of code


function printNum($max, $caller)
{
  for ($i=0; $i<$max; $i++ ) {
    echo " Scheduler :" . $caller . "  Print :" . $i . PHP_EOL;
  }
}
 
printNum(3, "caller1");
printNum(3, "caller2");
 
# output
 Scheduler :caller1  Print :0
 Scheduler :caller1  Print :1
 Scheduler :caller1  Print :2
 Scheduler :caller2  Print :0
 Scheduler :caller2  Print :1
 Scheduler :caller2  Print :2

Improved code after using synergy

First draft, manually adjust the generator execution


#  This code manually adjusts the sequence of the process execution code, of course, this code can be realized without coordination process, only using this process to explain the coordination process 
#  The generator gives us function interrupts, coroutines [ Generator send] Gives us the ability to reinvigorate the generator function 
function printNumWithGen($max)
{
  for ($i=0; $i<$max; $i++ ) {
    $res = yield $i;
    echo $res;
  }
}
 
$gen1 = printNumWithGen(3);
$gen2 = printNumWithGen(3);
 
//  Manual execution caller1  Again  caller2
$gen1->send(" Scheduler : caller1  Print :" . $gen1->current() . PHP_EOL);
$gen2->send(" Scheduler : caller2  Print :" . $gen2->current() . PHP_EOL);
 
//  Manual execution caller1  Again  caller2
$gen1->send(" Scheduler : caller1  Print :" . $gen1->current() . PHP_EOL);
$gen2->send(" Scheduler : caller2  Print :" . $gen2->current() . PHP_EOL);
 
//  Manual execution caller2  Again  caller1
$gen2->send(" Scheduler : caller2  Print :" . $gen2->current() . PHP_EOL);
$gen1->send(" Scheduler : caller1  Print :" . $gen1->current() . PHP_EOL);
 
# output
 Scheduler : caller1  Print :0
 Scheduler : caller2  Print :0
 Scheduler : caller1  Print :1
 Scheduler : caller2  Print :1
 Scheduler : caller2  Print :2
 Scheduler : caller1  Print :2

Summarize

The above cases should make everyone understand the significance of collaborative design and how to use collaborative design

Then we will have an automatic scheduler (Co automatic actuator) for our co-process, so there is no need to interrupt and resume manually


Related articles: