当前位置 : 主页 > 网络编程 > PHP >

PHP yield 异步操作结合同步业务代码例子

来源:互联网 收集:自由互联 发布时间:2021-07-03
?php/* * yield 异步操作结合同步业务代码例子 * @author Corzcombo_k@126.com *///业务同步代码function syncCode(){ var_dump((yield ['dns', 'www.baidu.com'])); var_dump((yield ['lag', 200])); var_dump((yield ['dns', 'www.taob
 
<?php
/*
 * yield 异步操作结合同步业务代码例子
 * @author Corz<combo_k@126.com>
 */
//业务同步代码
function syncCode()
{
    var_dump((yield ['dns', 'www.baidu.com']));
    var_dump((yield ['lag', 200]));
    var_dump((yield ['dns', 'www.taobao.com']));
    var_dump((yield ['sql', 'show tables']));
}
//异步调用器
function asyncCaller(Generator $gen)
{
    $r = $gen->current();
    if (isset($r)) {
        switch ($r[0]) {
            case 'sql':
                AsyncMysql::getInstance()->query($r[1], 
                    function ($retval) use($gen) {
                        $gen->send($retval);
                        asyncCaller($gen);
                    });
                break;
            case 'dns':
                swoole_async_dns_lookup($r[1], 
                    function ($host, $ip) use($gen) {
                        $gen->send([$host, $ip]);
                        asyncCaller($gen);
                    });
                break;
            case 'lag':
                swoole_timer_after($r[1], 
                    function () use($gen, $r) {
                        $gen->send('lag ' . $r[1] . 'ms');
                        asyncCaller($gen);
                    });
                break;
            default:
                $gen->send('no method');
                asyncCaller($gen);
                break;
        }
    }
}
  
asyncCaller(syncCode());
  
/**
 * 异步mysql类
 */
class AsyncMysql
{
  
    /**
     * @var mysqli
     */
    protected $db = null;
  
    /**
     * @var callable
     */
    protected $callable = null;
  
    public static function getInstance()
    {
        static $instance = null;
        return isset($instance) ? $instance : ($instance = new self());
    }
  
    public function __construct()
    {
        $this->db = new mysqli('127.0.0.1', 'root', '123456', 'mysql');
        swoole_event_add(swoole_get_mysqli_sock($this->db), [$this, 'onQuery']);
    }
  
    public function onQuery($db_sock)
    {
        $res = $this->db->reap_async_query();
        call_user_func($this->callable, $res->fetch_all(MYSQLI_ASSOC));
    }
  
    /**
     * @param string    $sql
     * @param callable  $callable
     */
    public function query($sql, callable $callable)
    {
        $this->callable = $callable;
        $this->db->query($sql, MYSQLI_ASYNC);
    }
}

网友评论