在Guzzle中进行池请求之间的竞赛 [英] Race between pool requests in Guzzle

查看:248
本文介绍了在Guzzle中进行池请求之间的竞赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用guzzle Pool执行多个api并发请求. 一切正常.

I'm doing mutiple api concurrent requests using guzzle Pool. Everything's working fine.

但是,如果有任何请求响应,我想停止/避免所有请求.也就是说,我想在请求之间进行一些竞争.可以在laravel中使用Guzzle吗?

But I want to stop/avoid all requests if any of the requests responded. That is, I want to do some race between the requests. Is it possible using Guzzle in laravel?

这是我到目前为止所做的:

Here's what I've done so far:

        $requests = function(array $urls){

                foreach ($urls as $url) {

                    yield new Request('GET', $url);

                }

        };


        $pool = new Pool($client, 
                        $requests($urls),

                        [
                            'concurrency' => 5,
                            'fulfilled' => function($response, $index) use ($urls){

                                echo "<br>Completed ".$urls[$index];

                            },

                            'rejected' => function($reason, $index){

                                echo "Rejected ".$index;


                            },
                        ]);


        $promise = $pool->promise();

        $promise->wait();

$ urls是一个URI数组

$urls is an array of URIs

推荐答案

我不认为当前的Guzzle Pool实现是不可能的.您唯一可以做的就是在fulfilled函数中的exit;:

I don't think it is possible with a current implementation of the Guzzle Pool. The only thing you can possibly do with it is to exit; in the fulfilled function:

'fulfilled' => function($response, $index) use ($urls){
    echo "Completed " . $urls[$index];
    exit;
 },

在这种情况下,它仍将发送所有请求,但立即以最快的响应退出脚本.

In this case it will still send all the requests, but immediately exit the script on the fastest response.

没有池,您可以使用 GuzzleHttp \ Promise \ any GuzzleHttp \ Promise \ some 辅助功能:

Without the Pool you can use GuzzleHttp\Promise\any or GuzzleHttp\Promise\some helper functions:

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => 'http://site.local/']);

// Initiate each request but do not block
$promises = [
    'delay3' => $client->getAsync('/async/delay3.php'),
    'delay2' => $client->getAsync('/async/delay2.php'),
    'delay1' => $client->getAsync('/async/delay1.php'),
];

//Initiate a competitive race between multiple promises
$promise = Promise\any($promises)->then(
    function (\GuzzleHttp\Psr7\Response $response) {
        echo "Completed: " . $response->getStatusCode() . "\n";
        echo $response->getBody() ."\n";
    },
    function ($reason) {
        echo $reason;
    }
);

$results = $promise->wait();

来自GuzzleHttp\Promise\some($count, $promises)的文档:

发起多个承诺或价值观之间的竞争竞赛 (价值将立即成为兑现的诺言).

Initiate a competitive race between multiple promises or values (values will become immediately fulfilled promises).

当完成诺言数量时,返回 通过包含实现的数组实现了promise 获奖者的价值按解决顺序排列.

When count amount of promises have been fulfilled, the returned promise is fulfilled with an array that contains the fulfillment values of the winners in order of resolution.

此承诺被{@see拒绝 GuzzleHttp \ Promise \ AggregateException}如果满足的数量 许诺少于所需的$ count.

This promise is rejected with a {@see GuzzleHttp\Promise\AggregateException} if the number of fulfilled promises is less than the desired $count.

来自GuzzleHttp\Promise\any($promises)的文档:

与some()类似,计数为1.但是,如果诺言兑现, 履行价值不是1的数组,而是直接的值.

Like some(), with 1 as count. However, if the promise fulfills, the fulfillment value is not an array of 1 but the value directly.

这篇关于在Guzzle中进行池请求之间的竞赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆