PHP implementation of the co-process

Source: Internet
Author: User
Tags abstract end functions generator new features return

In server programming, for asynchronous purposes, callback functions are required frequently, such as the following code

function Send ($value) {
    $data = process ($value);
    OnReceive ($data);

function OnReceive ($recv _value) {
    var_dump ($recv _value);
}

function process ($value) {return
    $value +1;
}

$send _value = 1;
Send ($send _value);



The implementation of the thing is very simple, in fact, is to send the send_value to the far end, the remote server to add a operation, sent back, so in the onreceive we can get the return value of the remote server Recv_value.

But such code will look fragmented, especially when it comes to making remote procedure calls again in the process, which becomes more difficult to develop and maintain. The coprocessor is designed to solve such problems, making the asynchronous code look synchronized.

Here's an example of using PHP's yield to complete code scheduling (if you want to understand this code, you need to first understand the new features of PHP 5.5 generator and yield)

The framework code is as follows:

Class Ccoroutine {/** * * @var generator/public $coroutine;

    /** * * @var miexed null or ccoroutine/public $father;
        Public function __construct ($coroutine, $father = null) {$this->coroutine = $coroutine;
    $this->father = $father;

    } class Asynctask {public $data;
    Public function __construct ($data) {$this->data = $data;

    } abstract class Coroutinescheduler {protected $coroutine = NULL;

    Abstract function send_and_receive ($value);
        Public function Run ($data) {$co = $this->send_and_receive ($data);
        $ccoroutine = new Ccoroutine ($CO);
    $this->schedule ($ccoroutine);
        } protected function Schedule ($ccoroutine) {$task = $ccoroutine->coroutine->current ();
            If the current value is null, it means that the $ccoroutine should have ended if (Is_null ($task)) {if (Is_null ($ccoroutine->father)) { has been completely scheduled to end--Generally oThe Nrecieve method runs to the last step return; else {//Note that if you run to this branch, it means that the child builder does not give the parent generator data//Kosheng builder may be passed by reference to change the value of the parent generator's variable//So this time, just dispatch the parent generator
                Can $ccoroutine->father->coroutine->next ();
                $father = $ccoroutine->father;
                $this->schedule ($father);
            Unset ($ccoroutine); } else {if (Is_object ($task) && $task instanceof Asynctask) {//When task is asynchronous data please
            When asked, start processing the socket and flameout the process here $this->dealtask ($task, $ccoroutine);
                ElseIf (Is_object ($task) && $task \generator) {//When a task is a builder, indicates that the current generator has a call to a child generator
                $NEWCC = new Ccoroutine ($task, $ccoroutine);
            $this->schedule ($NEWCC); ElseIf ($ccoroutine->father!= null) {//Note that if you run to this branch, the yield $str is invoked in the child's generator; /We stipulate that this type of writing is to pass data to the parent generator, so the current generator terminates the call and dispatches the parentGenerator $ccoroutine->father->coroutine->send ($task);
                $father = $ccoroutine->father;
                $this->schedule ($father);
            Unset ($ccoroutine);
        }} protected function Dealtask ($task, $ccoroutine) {$this->coroutine = $ccoroutine;
    $this->send ($task->data);
        The Public function send ($value) {$data = $this->process ($value);
    $this->onreceive ($data);
    The Public Function process ($value) {return $value +1;
        } protected function OnReceive ($data) {$this->coroutine->coroutine->send ($data);
    $this->schedule ($this->coroutine); }

}


The frame will send, onreceive, etc. functions are all encapsulated so that the caller's code can appear to be synchronized code

The caller code is as follows:

1. Need to implement Coroutinescheduler send_and_receive function, mainly in order to get the return value
class Solution extends Coroutinescheduler {

    Public Function send_and_receive ($data) {
        $result = (yield new Asynctask ($data));
        Var_dump ($result);
        
    }

2. At the outermost layer to invoke the frame code, give the input parameter $data
$s = new Solution ();
$data = 1;
$s->run ($data);





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.