http服务不适合用Coroutine\MySQL吧,但可以把sql交给Server->task()处理

新手上路,请多包涵

如果用CoroutineMySQL,每个进程始终要等协程运行完成,才能处理新的会话

阅读 1.4k
1 个回答

记录下,实际有多少核就设置worker_num为几,不然并发多出错。
在co::mysql初始化后,1秒大概5000内,结论是不用协程版读库还快且稳定,协程版常卡死

return array(
            'ip'=>'0.0.0.0',
            'port'=>'8088',
            'set'=>array(
                'max_conn'=>10000,      //测试用1000,布属时调整
                'worker_num' => 4,    //开启两个worker进程
                'max_request' => 0,   //每个worker进程处理max request重置
                'dispatch_mode'=>2,
                'user'=>'www',
                'group'=>'www',
                'enable_coroutine'=>true,
                'daemonize'=>0,
                #'task_worker_num'=>2,//配置异步Task进程的数量
            )
        );

        $http = new Swoole\Http\Server(C('ip'),C('port'));
        $http->set(C('set'));
        $http->on('request', function ($request, $response)use($http) {
                echo "\n".MysqlPool::count();
        $db = MysqlPool::get();
        $stmt = $db->prepare('SELECT * FROM je_admin where admin_id=?');
        $ret2 = $stmt->execute(array(1));
        var_dump($ret2);
        $response->header("Content-Type", "text/html; charset=utf-8");
        $response->end(print_r($ret2,true));
        MysqlPool::put($db);
        });
        $http->start();











class MysqlPool
{
    protected static function p()
    {
        static $pool=NULL;
        if($pool===NULL){
            $pool=new Swoole\Coroutine\Channel(self::len());
        }
        return $pool;
    }
    public static function len($decrease=0)
    {
        static $len=100;//定义最多多少个链接
        if($decrease!=0){
            $len--;
        }
        return $len;
    }
    public static function put($redis)
    {
        self::p()->push($redis);
    }
    
    public static function count()
    {
       return self::p()->length();
    }
    

    public static function get()
    {
        //有空闲连接 或都创建了  
        if (self::p()->length() > 0 || self::len()==0 ){
            return self::p()->pop();
        }
        self::len(-1);
        //无空闲连接,创建新连接
        $db = new co\MySQL();
        $ret = $db->connect(array(
            'host' => '127.0.0.1',
            'user' => 'root',
            'password' => 'xxxx',
            'database' => 'hjzfx_net',
        ));
        if(!$ret){
            echo "\n".'创建mysql新连接失败';
        }
        return $db;
    }
}

不用协程co:mysql,跑顺后(第二,三次后)5000也才0.5秒多

return array(
            'ip'=>'0.0.0.0',
            'port'=>'8088',
            'set'=>array(
                'max_conn'=>10000,      //测试用1000,布属时调整
                'worker_num' => 4,    //开启两个worker进程
                'max_request' => 0,   //每个worker进程处理max request重置
                'dispatch_mode'=>2,
                'user'=>'www',
                'group'=>'www',
                'enable_coroutine'=>true,
                'daemonize'=>0,
                #'task_worker_num'=>2,//配置异步Task进程的数量
            )
        );

        $http = new Swoole\Http\Server(C('ip'),C('port'));
        $http->set(C('set'));
        $http->on('request', function ($request, $response)use($http) {
            echo "\n".MysqlPool::count();
        $db = MysqlPool::get();
        $stmt = $db->prepare('SELECT admin_id,username FROM je_admin where admin_id=?');
        $stmt->bind_param('i', $a=1);
        $ret2 = $stmt->execute();
        $stmt->bind_result($admin_id,$username);
        $stmt->fetch();
        $stmt->close();
        printf ("\n%s (%s)", $admin_id, $username);
        $response->header("Content-Type", "text/html; charset=utf-8");
        $response->end(print_r($ret2,true));
        MysqlPool::put($db);
        });
        $http->start();
        
        
        
<?php
//RedisPool与Pool功能没区别 一个普通类 一个静态类使用方便
class MysqlPool
{
    protected static function p()
    {
        static $pool=NULL;
        if($pool===NULL){
            $pool=new Swoole\Coroutine\Channel(self::len());
        }
        return $pool;
    }
    public static function len($decrease=0)
    {
        static $len=100;//定义最多多少个链接
        if($decrease!=0){
            $len--;
        }
        return $len;
    }
    public static function put($redis)
    {
        self::p()->push($redis);
    }
    
    public static function count()
    {
       return self::p()->length();
    }
    

    public static function get()
    {
        //有空闲连接 或都创建了  
        if (self::p()->length() > 0 || self::len()==0 ){
            return self::p()->pop();
        }
        self::len(-1);
        $db = new mysqli('127.0.0.1', 'root', 'xxx', 'hjzfx_net');

        if($db->connect_error){
            echo "\n".'创建mysql新连接失败';
        }
        return $db;
    }
}

直接apache+php 0.8秒5000有时和协程版mysql一样卡死

$db = new mysqli('127.0.0.1', 'root', 'xxx', 'hjzfx_net');
$stmt = $db->prepare('SELECT admin_id,username FROM je_admin where admin_id=?');
$stmt->bind_param('i', $a=1);
$ret2 = $stmt->execute();
$stmt->bind_result($admin_id,$username);
$stmt->fetch();
$stmt->close();
printf ("\n%s (%s)", $admin_id, $username);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进