zoukankan      html  css  js  c++  java
  • 使用swoole多进程案例【一】

    1.管道通讯

    <?php
        class BaseProcess{
            private $process;
            public function __construct(){
                $this->process = new swoole_process(array($this,'run'),false,true);
                $this->process->start();
    
                swoole_event_add($this->process->pipe,function($pipe){
                    $data = $this->process->read();
                    echo $data.PHP_EOL;
                });
            }
    
            public function run($worker){
                swoole_timer_tick(1000,function($time_id){
                    static $index =0;
                    $index= $index+1;
                    $this->process->write($index);
                    var_dump($index);
                    if($index == 10){
                        swoole_timer_clear($time_id);
                    }
                });
            }
        }
    
        for($i=0;$i<2;$i++){
            new BaseProcess();
            sleep(1);
        }
        //使用Process作为监控父进程,创建管理子进程时,父类必须注册信号SIGCHLD对退出的进程执行wait操作
        swoole_process::signal(SIGCHLD,function($sig){
            while($ret = swoole_process::wait(false)){
                echo "PID={$ret['pid']}".PHP_EOL;
            }
        });
    ?>

     2.消息队列

    #swoole使用消息队列实现进程之间的通讯
        class BaseProcess{
            private $process;
            public function __construct(){
                $this->process = new swoole_process(array($this,'run'),false,true);
                if(!$this->process->useQueue(123)){
                    var_dump(swoole_strerror(swoole_errno()));
                    exit;
                }
                $this->process->start();
                while(true){
                    $data = $this->process->pop();
                    echo $data.PHP_EOL;
                }
            }
    
            public function run(){
                swoole_timer_tick(1000,function($timer_id){
                    static $index=0;
                    $index = $index+1;
                    $this->process->push("hello");
                    if($index==10){
                        swoole_timer_clear($timer_id);
                    }
                });
            }
        }
        new BaseProcess();
        swoole_process::signal(SIGCHLD,function($sig){
            while($ret = swoole_process::wait(false)){
                $pid = $ret['pid'];
                echo "pid = $pid";
            }
        });
  • 相关阅读:
    flutter 中 List 和 Map 的用法
    AndroidStudio中Flutter打包APK
    flutter中的异步机制 Future
    [控件] 画饼状图的控件
    用CAShapeLayer写股市K线图动画效果
    使用Instruments中的CoreAnimation分析动画
    山寨Facebook的Shimmer效果
    maskView遮罩中多张图片的动画
    将CAGradientLayer用作maskView的遮罩图层
    [翻译] JKLLockScreenViewController
  • 原文地址:https://www.cnblogs.com/zh718594493/p/12900928.html
Copyright © 2011-2022 走看看