zoukankan      html  css  js  c++  java
  • swoole异步MySql

    1:先创建服务端文件MySql.php再创建数据库以及简单的数据表

    <?php
    use Swoole\Runtime;
    use Swoole\Coroutine;
    use function Swoole\Coroutine\run;
    class MySql
    {
        
        public function go(){
            Runtime::enableCoroutine();
            $s = microtime(true);
            Co\run(function() {
                
                for ($c = 50; $c--;) {
                    go(function(){
                        $pdo = new PDO('mysql:host=127.0.0.1;dbname=demo;charset=utf8', 'demo', 'demo');
                        $statement = $pdo->prepare('SELECT `demo` FROM `demo` where id = 1');
                        for ($n = 100; $n--;) {
                            $statement->execute();
                            $res = $statement -> fetch();
                            var_dump('a:' . $res['user_name']);
                        }
                    });
                }
                
                for ($c = 50; $c--;) {
                    go(function(){
                        $pdo = new PDO('mysql:host=127.0.0.1;dbname=demo;charset=utf8', 'demo', 'demo');
                        $statement = $pdo->prepare('SELECT `demo` FROM `demo` where id = 1');
                        for ($n = 100; $n--;) {
                            $statement->execute();
                            $res = $statement -> fetch();
                            var_dump('b:' . $res['user_name']);
                        }
                    });
                }
                
                
            });
            
            var_dump('用时:' . (microtime(true) - $s) . ' s');
            
        }
        
        
    }
    $mysql = new MySql();
    $mysql->go();

    2:启动服务php MySql.php 会发现输出的a里面掺杂b,b里面掺杂1,耗时短

  • 相关阅读:
    linux 内核配置
    使用 git 下载linux 源码
    订阅 linux 邮件列表注意的问题
    使用反射创建一维数组和二维数组
    反射API
    反射机制
    集合案例--对ArrayList容器中的内容进行排序
    Collections
    TreeSet
    Set容器
  • 原文地址:https://www.cnblogs.com/sunny20/p/15632299.html
Copyright © 2011-2022 走看看