美文网首页
swoole_process,但没有明白freeQueue的用法

swoole_process,但没有明白freeQueue的用法

作者: 北派浩 | 来源:发表于2018-05-11 14:19 被阅读0次

    <?php

    swoole = new swoole_server("0.0.0.0",9503);

            $this->swoole->set($this->config());

            $this->swooleInit();

            $this->swoole->start();

        }

        /**

        * swoole配置

        */

        public function config()

        {

            $config = [

                        'reactor_num'=>1, //默认设置为CPU核数,调节poll线程的数量,以充分利用多核,reactor_num必须小于或等于worker_num

                        'daemonize'=>true,//转入后台作为守护进程运行

                        'worker_num'=>2, //设置启动的worker进程数量,类似于php-fpm的个数,每个进程占用40M内存

                        'max_request'=>1000, //单个线程最大请求数

                        'max_conn'=>1000, //最多维持1000个tcp链接

                        'dispatch_mode'=>3, //worker进程数据包分配模式1平均分配,2按FD取模固定分配,3抢占式分配,默认为取模(dispatch=2)

                        // 'task_worker_num'=>1, //务必要注册onTask、onFinish2个事件回调函数,也是数据库连接池的保证

                        'open_eof_check'=>true,

                        'package_eof'=>PHP_EOL,

                        'open_eof_split'=>true

                        // 'log_file'=>'/log',

                    ];

            return $config;

        }

        public function swooleInit()

        {

            $this->swoole->on("start",array($this,"onStart"));

            $this->swoole->on("connect",array($this,"onConnect"));

            $this->swoole->on("receive",array($this,"onReceive"));

            $this->swoole->on("connect",array($this,"onConnect"));

            $this->swoole->on("close",array($this,"onClose"));

        }

        public function onStart($server)

        {

            echo 'hello';

        }

        public function onConnect($server,$reactor_id)

        {

            echo 'world'; 

        }

        public function onReceive($server,$fd,$reactor_id ,$data)

        {

            //这里创建进程

            for($i = 0; $i< $this->worker_nums; $i++)

            {

                $process = new swoole_process(array($this,'onProcess'),false,false);

                $process->useQueue();

                $pid = $process->start();

                $this->workers[$pid] = $process;

            }

            //循环队列

            foreach ($this->workers as $pid=>$worker)

            {

                $process->push("hello{$pid}");

                $result = $process->pop();

                echo "From worker: $result\n";//这里主进程,接受到的子进程的数据

            }

            //释放

            for($i=0; $i < $this->worker_nums; $i++)

            {

                $ret = swoole_process::wait();

                $pid = $ret['pid'];

                unset($this->worker_nums[$pid]);

                echo "Worker Exit, PID=".$pid.PHP_EOL;

            }

        }

        public function onProcess($worker)

        {

            $recv  = $worker->pop();

            echo "FROM master {$recv}\n";

            $worker->push("heheh parent");

            $worker->exit(0);

        }

        public function onClose($server,$fd,$reactor_id)

        {

            echo 'close';

        }

    }

    $server = new server();

    ?>

    相关文章

      网友评论

          本文标题:swoole_process,但没有明白freeQueue的用法

          本文链接:https://www.haomeiwen.com/subject/ttufdftx.html