返回介绍

抽象异步模型

发布于 2024-08-15 12:54:37 字数 3198 浏览 0 评论 0 收藏 0

抽象异步模型

对回调模型抽象出异步接口Async

只有一个方法的接口通常都可以使用闭包代替,区别在于interface引入新类型,闭包则不会。如果说thunkify依赖了参数顺序的弱约定,Async相对严肃的依赖了类型。

<?php

interface Async
{
    public function begin(callable $callback);
}

// AsyncTask符合Async定义, 实现Async
final class AsyncTask implements Async
{
    public function next($result = null)
    {
        $value = $this->gen->send($result);

        if ($this->gen->valid()) {
            // \Generator -> Async
            if ($value instanceof \Generator) {
                $value = new self($value);
            }

            if ($value instanceof Async) {
                $async = $value;
                $continuation = [$this, "next"];
                $async->begin($continuation);
            } else {
                $this->next($value);
            }

        } else {
            $cc = $this->continuation;
            $cc($result);
        }
    }
}

两个简单的对回调接口转换例子:

<?php

// 定时器修改为标准异步接口
class AsyncSleep implements Async
{
    public function begin(callable $cc)
    {
        swoole_timer_after(1000, $cc);
    }
}

// 异步dns查询修改为标准异步接口
class AsyncDns implements Async
{
    public function begin(callable $cc)
    {
        swoole_async_dns_lookup("www.baidu.com", function($host, $ip) use($cc) {
            // 这里我们会发现, 通过call $cc, 将返回值作为参数进行传递, 与callcc相像
            // $ip 通过$cc 从子生成器传入父生成器, 最终通过send方法成为yield表达式结果
            $cc($ip);
        });
    }
}

function newGen()
{
    $r1 = (yield newSubGen());
    $r2 = (yield 2);
    $start = time();
    yield new AsyncSleep();
    echo time() - $start, "\n";
    $ip = (yield new AsyncDns());
    yield "IP: $ip";
}
$task = new AsyncTask(newGen());

$trace = function($r) { echo $r; };
$task->begin($trace);
// output:
// 1
// IP: 115.239.210.27

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文