• mysql的mysqli异步与php的携程


    <?php
    
    $begin = time();
    
    //同步请求
    function multi_sync(){
        $host = '192.168.2.87';
        $user = 'census';
        $password = 'census';
        $database = 'census';
        $mysqli = new mysqli($host, $user, $password, $database);
        
        $query   =  "SELECT sleep(1);" ;
        $query  .=  "SELECT sleep(1);" ;
    
        if ( $mysqli -> multi_query ( $query )) {
            do {
                echo "multi_sync_query 
    ";
                if ( $result  =  $mysqli -> store_result ()) {
                    $ret = $result->fetch_array(MYSQLI_ASSOC);
                    var_dump($ret);
                }
                // if ( $mysqli -> more_results ()) {
                     // printf ( "-----------------
    " );
                // }
            } while ( $mysqli -> more_results () && $mysqli -> next_result ());
        }
    }
    multi_sync();
    
    //同步请求
    /*
    function sync(){
        $host = '192.168.2.87';
        $user = 'census';
        $password = 'census';
        $database = 'census';
        $db = new mysqli($host, $user, $password, $database);
        echo "sync_query 1
    ";
        $statement1 = $db->query('select sleep(1)');
        $ret1 = $statement1->fetch_array(MYSQLI_ASSOC);
        echo "sync_query 2
    ";
        $statement2 = $db->query('select sleep(1)');
        $ret2 = $statement2->fetch_array(MYSQLI_ASSOC);
        var_dump($ret1);
        var_dump($ret2);
    }
    sync();
    */
    
    //携程异步---php携程,mysqlnd异步
    /*
    function f1(){
        $db = new db();
        $obj = $db->async_query('select sleep(1)');
        echo "f1 async_query 
    ";
        yield $obj;
        $row = $db->fetch();
        echo "f1 fetch
    ";
        yield $row;
    }
    
    function f2(){
        $db = new db();
        $obj = $db->async_query('select sleep(1)');
        echo "f2 async_query
    ";
        yield $obj;
        $row = $db->fetch();
        echo "f2 fetch
    ";
        yield $row;
    }
    
    $gen1 = f1();
    $gen2 = f2();
    
    $gen1->current();
    $gen2->current();
    
    
    $gen1->next();
    $gen2->next();
    
    $ret1 = $gen1->current();
    $ret2 = $gen2->current();
    
    var_dump($ret1);
    var_dump($ret2);
    */
    echo time() - $begin;
    
    class db{
        static $links;
        private $obj;
    
        function getConn(){
            $host = '192.168.2.87';
            $user = 'census';
            $password = 'census';
            $database = 'census';
            $this->obj = new mysqli($host, $user, $password, $database);
            self::$links[spl_object_hash($this->obj)] = $this->obj;
            return self::$links[spl_object_hash($this->obj)];
        }
    
        function async_query($sql){
            $link = $this->getConn();
            $link->query($sql, MYSQLI_ASYNC);
            return $link;
        }
    
        function fetch(){
            //for($i = 1; $i <= 5; $i++){
                $read = $errors = $reject = self::$links;
                $re = mysqli_poll($read, $errors, $reject, 1);
                foreach($read as $obj){
                    if($this->obj === $obj){
                        $sql_result = $obj->reap_async_query();
                        $sql_result_array = $sql_result->fetch_array(MYSQLI_ASSOC);//只有一行
                        $sql_result->free();
                        return $sql_result_array;
                    }
                }
            //}
        }
    }

    异步之后只要1秒,同步以及同步多请求,都是2秒

  • 相关阅读:
    初等数论初步——剩余类及其运算
    初等数论初步——同余的概念及性质
    救济金发放(模拟题打卡)
    寻找最小字典序字符串(水题打卡)
    杭电3790最短路径问题
    Android网络请求
    js制造运动的假象-------Day63
    数据切分——原理
    Android 开发中 iBeacon的使用
    HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
  • 原文地址:https://www.cnblogs.com/kudosharry/p/5652489.html
Copyright © 2020-2023  润新知