账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    在使用swoole_process时在子进程使用复杂的正则子进程会crash掉,主进程没事。
    24
    0

    在使用swoole_process时在子进程使用复杂的正则子进程会crash掉,主进程没事。

    相关代码

    <?php
    class TestProcess{
        public $mpid=0;
        public $max_precess=8;
        public $task  = array();
        public $works = [];
        public $swoole_table = NULL;
        public $process_objs = [];
        
        function handle($index, $worker){
            while (true){
                $msg = "test_preg_replace_slave";
                $msg = preg_replace('/[ \r\n\t]*\n[ \r\n\t]*/', '', $msg);//使用会crash
                //$msg = preg_replace('/[ \r\n\t]*\n/', '', $msg);//正常
                var_dump($msg);
            }
        }
    
        public function __construct(){
            try {
                $this->swoole_table = new swoole_table(1024);
                $this->swoole_table->column('index', swoole_table::TYPE_INT);
                $this->swoole_table->create();
                $this->mpid = posix_getpid();
            }catch (\Exception $e){
                Logger::error($e);
            }
        }
    
        public function run(){
            try{
                for ($i=0; $i < $this->max_precess; $i++) {
                    $this->createProcess();
                }
                $this->runProcess();
                $this->processWait();
            }  catch (Exception $e){
                Logger::error($e);
                exit();
            }
        }
    
        private function runProcess(){
            foreach($this->process_objs as $index => $process){
                $this->works[$index]=$process->start();
            }
        }
    
        public function createProcess($index=null){
            $msg = "test_preg_replace_master";
            $msg = preg_replace('/[ \r\n\t]*\n[ \r\n\t]*/', '', $msg);
            var_dump($msg);
            if(is_null($index)){
                $index=$this->swoole_table->get('index');
                if($index === false){
                    $index = 0;
                }else{
                    $index = $index['index']+1;
                }
            }
            $this->swoole_table->set('index',array('index'=>$index));
            $process = new swoole_process(function(swoole_process $worker)use($index){
                call_user_func_array(array($this, 'handle'),array($index, $worker));
            }, false, false);
            $this->process_objs[$index] = $process;
        }
    
        public function processWait(){
            $ret = array();
            while($ret = swoole_process::wait()) {
                $pid = $ret['pid'];
            }
        }
        
    }
    try{
        $obj = new TestProcess();
        $obj->run();
    } catch (Exception $e) {
        Logger::error($e->getMessage());
    }

    swoole 版本 4.3.0-alpha
    php 版本 7.3.0
    开启coredump时候,发现文件巨大,每个子进程文件达1.4G

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部