微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Amp PHP 非阻塞并发框架

程序名称:Amp

授权协议: MIT

操作系统: 跨平台

开发语言: PHP

Amp 介绍

Amp 是一个 PHP 非阻塞并发框架,它提供了一个事件循环,promise 和 stream 作为异步编程的基础。与生成器结合使用的 promise
用于构建协程,它允许像同步代码一样编写异步代码,而不需要任何回调。

demo:

<?PHP

use Amp\Artax\Response;
use Amp\Loop;

require __DIR__ . '/../vendor/autoload.PHP';

Loop::run(function () {
    $uris = [
        "https://google.com/",
        "https://github.com/",
        "https://stackoverflow.com/",
    ];

    $client = new Amp\Artax\DefaultClient;
    $client->setoption(Amp\Artax\Client::OP_disCARD_BODY, true);

    try {
        foreach ($uris as $uri) {
            $promises[$uri] = $client->request($uri);
        }

        $responses = yield $promises;

        foreach ($responses as $uri => $response) {
            print $uri . " - " . $response->getStatus() . $response->getReason() . PHP_EOL;
        }
    } catch (Amp\Artax\HttpException $error) {
        // If something goes wrong Amp will throw the exception where the promise was yielded.
        // The Client::request() method itself will never throw directly, but returns a promise.
        print $error->getMessage() . PHP_EOL;
    }
});

Amp 官网

https://amphp.org/amp

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐