PHP AMP,Amp-PHP 非阻塞并发框架

软件简介

Amp 是一个 PHP 非阻塞并发框架,它提供了一个事件循环,promise 和 stream 作为异步编程的基础。与生成器结合使用的 promise

用于构建协程,它允许像同步代码一样编写异步代码,而不需要任何回调。

demo:

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;

}

});

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值