简体   繁体   中英

Use Guzzle pool instead of 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; 
    }

I have the below working example for guzzle 6. I use postAsync and 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();
}

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

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


        }


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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