Home  >  Article  >  Backend Development  >  Code example of swoole_process parent-child process pipeline communication

Code example of swoole_process parent-child process pipeline communication

不言
不言forward
2019-03-04 13:50:232757browse

This article brings you code examples about swoole_process parent-child process pipeline communication. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Without further ado, let’s get straight to the code

Created child process:

public function __construct()
    {
        $this->redis   = Container::get(SwooleRedis::class);//获取异步redis获取更高性能
        $this->process = new swoole_process(function (swoole_process $process) {
            return $this->process($process);
        }, false, SOCK_DGRAM);
        $this->process->name('Test_Gateway');
        $this->process->useQueue();
        $this->process->start();//启动子进程
    }
    
    /**
     * 子进程处理逻辑
     * @param swoole_process $process
     */
    private function process(swoole_process $process)
    {
        $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
        
        $client->on("connect", function (swoole_client $cli) use ($process) {
            $process->write('connected');
        });
        
        $client->on("receive", function (swoole_client $cli, $data) use ($process) {
            $process->write($data);
        });
        
        $client->on("error", function (swoole_client $cli) use ($process) {
            $process->write('error');
        });
        
        $client->on("close", function (swoole_client $cli) use ($process) {
            $process->write('close');
        });
        
        if ($client->connect('127.0.0.1', 90, -1)) {
        
        } else {
            $process->write('网关连接失败');
        }
        
        swoole_event_add($process->pipe,
            function ($pipe) use ($process, $client) {//读取父进程管道消息
                $client->send($process->read());
            });
    }

Parent process onWorkerStart:

/**
     * @param swoole_server $serv
     * @param               $worker_id
     */
    public function onWorkerStart(\swoole_server $serv, $worker_id)
    {
        if ($worker_id === 0) {
            swoole_timer_tick(1000, function () {
                $this->process->write('ping');
            });
            $process = $this->process;
            swoole_event_add($process->pipe,
                function ($pipe) use ($process) {//获取子进程的管道消息
                    echo "子进程消息:" . $process->read() . PHP_EOL;
                });
        }
    }
  • The client of the child process can be ignored. This demo is just an example of masking pipe communication.
  • You cannot use the message queue when using pipes: $process_push() and $process->pop();
  • Theoretically, by registering an event_loop in each of the parent and child processes, you can send messages and receive them at the same time.
  • Other follow-up additions

The above is the detailed content of Code example of swoole_process parent-child process pipeline communication. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete