Cooperative application of swoole

Keywords: PHP Redis PhpStorm

  1. The use of coroutine and goroutine of golang
  1. coroutine's process is released after the trunk is finished, and other processes are executed Automatically exit after goroutine's trunk ends
  1. When the coroutine process of swoole is used, if a dead loop is used, the trunk is running all the time, and resources will not be released. Therefore, co::sleep() is needed to actively release resources to other processes for execution

Channel (pipeline)

In the php business scenario, the communication between multiple processes is usually achieved through redis memory cache We can use Channel to realize the communication between the processes. When comparing with php multiprocessing, we can compare Channel to redis queue

/**
 * Describe: Say important things several times
 * Created by PhpStorm.
 * User: querying
 * Date: 18-8-21
 * Time: 11:34 a.m.
 */
use Swoole\Channel;

Class Demo
{

    public function startK()
    {

        // Task data pipeline
        $chan = new Channel(1);

        //The pipeline is used to detect whether it needs to jump out of the dead cycle
        $pushStatusChan = new Channel(1);
        $popStatusChan  = new Channel(1);
        $logStatusChan  = new Channel(1);

        go(function () use ($chan, $pushStatusChan) {
            while (1) {
                if ($pushStatusChan->pop() === 'break') {
                    break;
                }
                \Co::sleep(1);
                $data = $this->getPushData();

                if (!empty($data)) {
                    $chan->push($data);
                    var_dump("data");
                }
            }
        });

        go(function () use ($chan, $popStatusChan) {
            while (1) {
                if ($popStatusChan->pop() === 'pop:break') {
                    break;
                }
                //Voluntary surrender
                \Co::sleep(1);
                $data = $chan->pop();
                if (!empty($data)) {
                    var_dump("Create Association");
                    go(function () use ($data) {
                        $this->doTask($data);
                    });
                    var_dump("After creating a collaboration");
                }
            }
        });

        go(function () use ($chan, $logStatusChan) {
            while (1) {
                if ($logStatusChan->pop() === 'break') {
                    break;
                }
                $coStatus = \Co::stats();
                var_dump("Association number:{$coStatus['coroutine_num']}");
                $this->memoryLog();
                //Every 5 seconds, the recording process takes up memory
                \Co::sleep(5);
            }
        });

        go(function () use ($pushStatusChan, $popStatusChan, $logStatusChan) {

            $now_hour = -1;
            while (1) {

                if ($now_hour !== date('H')) {
                    $now_hour = date('H');
                }

                //Process state detection
                $result = $this->processStatusCheck();
                if (!$result) {
                    //push out
                    $pushStatusChan->push('break');
                    //pop process jump out
                    $popStatusChan->push("break");
                    //Memory log Association
                    $logStatusChan->push('break');
                    break;
                }
                \Co::sleep(3);
            }
        });

    }

    public function processStatusCheck()
    {
        // todo: verify process status
        return true;
    }

    public function memoryLog()
    {
        // todo: record the memory used by the current process

    }

    public function doTask($data)
    {
        // todo: get the pipeline data and process the business according to the data processing

    }

    public function getPushData()
    {
        // todo: get the data to be put into the pipeline

        return [
            'test' => '666'
        ];
    }
}



Posted by doofystyle on Fri, 03 Jan 2020 11:06:56 -0800