首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Guzzle而不是guzzle

使用Guzzle而不是guzzle
EN

Stack Overflow用户
提问于 2019-03-05 17:22:21
回答 3查看 1.2K关注 0票数 2

我使用guzzle发送并发请求,但我想控制并发,这就是为什么我想使用guzzle池。我如何将豪饮承诺转化为豪饮池塘。下面是我的代码:

代码语言:javascript
运行
复制
 public function getDispenceryforAllPage($dispencery)
    {
        $GetAllproducts = [];
        $promiseGetPagination = $this->client->getAsync($dispencery)
            ->then(function ($response) {
                return $this->getPaginationNumber($response->getBody()->getContents());           
                });

               $Pagination = $promiseGetPagination->wait();

                $pagearray = array();

                    for($i=1;$i<=$Pagination; $i++){
                        $pagearray[] = $i;

                        }


                foreach($pagearray as $page_no) {

                        $GetAllproducts[] = $this->client->getAsync($dispencery.'?page='.$page_no)
                        ->then(function ($response) {

                            $promise =  $this->getData($response->getBody()->getContents()); 
                            return $promise;       
                            });


        }
       $results =  GuzzleHttp\Promise\settle($GetAllproducts)->wait();
        return $results; 
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-06 19:58:40

只需对生成器使用each_limit()each_limit_all() (而不是settle())即可。

代码语言:javascript
运行
复制
function getDispenceryforAllPage($dispencery)
{
    $promiseGetPagination = $this->client->getAsync($dispencery)
        ->then(function ($response) {
            return $this->getPaginationNumber($response->getBody()->getContents());
        });

    $Pagination = $promiseGetPagination->wait();

    $pagearray = range(1, $Pagination);

    $requestGenerator = function () use ($dispencery, $pagearray) {
        foreach ($pagearray as $page_no) {
            yield $this->client->getAsync($dispencery . '?page=' . $page_no)
                ->then(function ($response) {
                    return $this->getData($response->getBody()->getContents());
                });
        }
    };

    // Max 5 concurrent requests
    $results = GuzzleHttp\Promise\each_limit_all($requestGenerator(), 5)->wait();

    return $results;
}
票数 0
EN

Stack Overflow用户

发布于 2020-08-25 12:38:28

我有下面的Grouble6的工作示例,我使用postAsync和pool。

代码语言:javascript
运行
复制
function postInBulk($inputs)
{
    $client = new Client([
        'base_uri' => 'https://a.b.com'
    ]);
    $headers = [
        'Authorization' => 'Bearer token_from_directus_user'
    ];

    $requests = function ($a) use ($client, $headers) {
        for ($i = 0; $i < count($a); $i++) {
            yield function() use ($client, $headers) {
                return $client->postAsync('https://a.com/project/items/collection', [
                    'headers' => $headers,
                    'json' => [
                        "snippet" => "snippet",
                        "rank" => "1",
                        "status" => "published"
                    ]        
                ]);
            };
        }
        
    };

    $pool = new Pool($client, $requests($inputs),[
        'concurrency' => 5,
        'fulfilled' => function (Response $response, $index) {
            // this is delivered each successful response
        },
        'rejected' => function (RequestException $reason, $index) {
            // this is delivered each failed request
        },
    ]);

    $pool->promise()->wait();
}
票数 1
EN

Stack Overflow用户

发布于 2019-04-25 21:27:36

我已经修改了你的代码来支持池。

代码语言:javascript
运行
复制
class GuzzleTest
{
    private $client;

    public function __construct($baseUrl)
    {
        $this->client = new \GuzzleHttp\Client([// Base URI is used with relative requests
            'base_uri' => $baseUrl,
            // You can set any number of default request options.
            'timeout'  => 2.0,]);

    }


    public function getDispenceryforAllPage($dispencery)
    {
        $GetAllproducts = [];
        $promiseGetPagination = $this->client->getAsync($dispencery)
            ->then(function ($response) {
                return $this->getPaginationNumber($response->getBody()->getContents());
            });

        $Pagination = $promiseGetPagination->wait();

        $pagearray = array();

        for ($i = 1; $i <= $Pagination; $i++) {
            $pagearray[] = $i;

        }


        $pool = new \GuzzleHttp\Pool($this->client, $this->_yieldRequest($pagearray, $dispencery), [
            'concurrency' => 5,
            'fulfilled' => function ($response, $index) {
                // this is delivered each successful response

            },
            'rejected' => function ($reason, $index) {
                // this is delivered each failed request
            },
        ]);

        // Initiate the transfers and create a promise
        $poolPromise = $pool->promise();

        // Force the pool of requests to complete.
        $results = $poolPromise->wait();



        return $results;
    }

    private function _yieldRequest($pagearray, $dispencery){

        foreach ($pagearray as $page_no) {

            $uri = $dispencery . '?page=' . $page_no;

            yield function() use ($uri) {
                return $this->client->getAsync($uri);
            };


        }


    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54999357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档