繁体   English   中英

使用 Guzzle 池而不是 Guzzle 承诺

[英]Use Guzzle pool instead of guzzle promises

我正在使用 guzzle 承诺发送并发请求,但我想控制并发性,这就是我想使用 guzzle 池的原因。 我如何将 guzzle 承诺转化为 guzzle 池。 这是我的代码:

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

我有以下 guzzle 6 的工作示例。我使用 postAsync 和 pool。

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

只需将each_limit()each_limit_all() (而不是each_limit_all() settle() )与生成器一起使用。

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

我已修改您的代码以支持池。

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


        }


    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM