duangu6588
2019-03-05 09:22
浏览 590
已采纳

使用Guzzle池代替guzzle promises

I am using guzzle promises to send a concurrent request but I want to control the concurrency that's why I want to use guzzle pool. How I can transform guzzle promises into guzzle pool. here is my code:

 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; 
    }

图片转代码服务由CSDN问答提供 功能建议

我使用guzzle promises发送并发请求,但我想控制并发性,这就是为什么我要使用guzzle 池。 我怎样才能将枪口的承诺变成枪口。 这是我的代码:

  public function getDispenceryforAllPage($ dispencery)
 {
 $ GetAllproducts = []; 
 $ promiseGetPagination = $ this-&gt; client-&gt  ; getAsync($ dispencery)
  - &gt;然后(函数($ response){
返回$ this-&gt; getPaginationNumber($ response-&gt; getBody() - &gt; getContents()); 
});  
 
 $ Pagination = $ promiseGetPagination-&gt; wait(); 
 
 $ pagearray = array(); 
 
 for($ i = 1; $ i&lt; = $ Pagination; $ i ++){\  n $ pagearray [] = $ i; 
 
} 
 
 
 foreach($ pagearray as $ page_no){
 
 $ GetAllproducts [] = $ this-&gt; client-&gt; getAsync($  dispencery。'?page ='。$ page_no)
  - &gt; then(function($ response){
 
 $ promise = $ this-&gt; getData($ response-&gt; getBody() - &gt; getContents  ()); 
返回$ promise; 
  }}; 
 
 
} 
 $ results = GuzzleHttp \ Promise \ settle($ GetAllproducts) - &gt; wait(); 
返回$ results;  
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanqian3953 2019-03-06 11:58
    已采纳

    Just use each_limit() or each_limit_all() (instead of settle()) with a generator.

    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;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhan8610 2019-04-25 13:27

    I have modified your code to support pool.

    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);
                };
    
    
            }
    
    
        }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题