PHP implementation co-process

Source: Internet
Author: User

In server programming, in order to achieve asynchronous, the recurrent need for callback functions, 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 send_value to the remote, the remote server to add a operation, send back, so in onreceive we can get the remote server return value Recv_value.


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


Here is an example of how to do code scheduling using the yield of PHP (if you want to understand this code, you need to first understand PHP 5.5 's new features 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, the $ccoroutine should have ended if (Is_null ($task)) {if (Is_null ($ccoroutine->father)) {        has been completely dispatched to the end--Generally the Onrecieve method runs to the last step of return;    } else {//Note that if you run to this branch, it means that the child generator does not pass the data to the parent generator//Stepson builder may be passed by reference to change the value of the parent generator's variable//So this time just dispatch the parent generator                It can be $ccoroutine->father->coroutine->next ();                $father = $ccoroutine->father;                $this->schedule ($father);            Unset ($ccoroutine); }} else {if (Is_object ($task) && $task instanceof Asynctask) {//When the task is an asynchronous data request            To start processing the socket and stall the process here $this->dealtask ($task, $ccoroutine);                } elseif (Is_object ($task) && $task instanceof \generator) {//When the task is a generator, indicates that the current generator has a call to the child builder                $NEWCC = new Ccoroutine ($task, $ccoroutine);            $this->schedule ($NEWCC); } elseif ($ccoroutine->father! = null) {//Note that if you run to this branch, the yield $str is called in the generator of the child; It is stipulated that this notation is to pass data to the parent generator, so the current generator terminates the call to dispatch the parent generator $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 framework encapsulates the functions of Send, onreceive, and so on, so that the caller's code can look like a synchronous code

The caller code is as follows:

1. Need to implement Coroutinescheduler's send_and_receive function, mainly to get the return value in the class solution extends Coroutinescheduler {public    function Send_and_receive ($data) {        $result = (yield new Asynctask ($data));        Var_dump ($result);            }} 2. To call the framework's code at the outermost, give the input parameters $data $s = new solution (), $data = 1; $s->run ($data);





PHP implementation co-process

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.