Php 使用“狂饮池”而不是“狂饮承诺”

Php 使用“狂饮池”而不是“狂饮承诺”,php,httprequest,guzzle,guzzle6,Php,Httprequest,Guzzle,Guzzle6,我使用guzzle承诺发送并发请求,但我希望控制并发性,这就是我希望使用guzzle池的原因。我怎样才能把狂饮的承诺变成狂饮的水池。这是我的密码: public function getDispenceryforAllPage($dispencery) { $GetAllproducts = []; $promiseGetPagination = $this->client->getAsync($dispencery)

我使用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; 
    }
公共函数getDispenceryforAllPage($dispencery)
{
$GetAllproducts=[];
$promiseGetPagination=$this->client->getAsync($dispencery)
->然后(函数($response){
返回$this->getPaginationNumber($response->getBody()->getContents());
});
$Pagination=$promiseGetPagination->wait();
$pagearray=array();
对于($i=1;$iclient->getAsync($dispencery.'?page='.$page\u no)
->然后(函数($response){
$promise=$this->getData($response->getBody()->getContents());
退还$PROFECT;
});
}
$results=GuzzleHttp\Promise\settle($GetAllproducts)->wait();
返回$results;
}
只需在生成器中使用或
每个_limit_all()
(而不是
结算()

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


        }


    }
}
class-guzzle测试
{
私人客户;
公共函数构造($baseUrl)
{
$this->client=new\GuzzleHttp\client([//基本URI用于相对请求
'base_uri'=>$baseUrl,
//您可以设置任意数量的默认请求选项。
“超时”=>2.0,]);
}
公共函数getDispenceryforAllPage($dispencery)
{
$GetAllproducts=[];
$promiseGetPagination=$this->client->getAsync($dispencery)
->然后(函数($response){
返回$this->getPaginationNumber($response->getBody()->getContents());
});
$Pagination=$promiseGetPagination->wait();
$pagearray=array();
对于($i=1;$i客户端,$this->\u yieldRequest($pagearray,$dispencery)[
“并发”=>5,
“已完成”=>函数($response,$index){
//每一次成功的响应都会传递此信息
},
“拒绝”=>函数($reason,$index){
//这将在每个失败的请求中传递
},
]);
//启动转移并创建承诺
$poolPromise=$pool->promise();
//强制完成请求池。
$results=$poolPromise->wait();
返回$results;
}
私有函数_yieldRequest($pagearray,$dispencery){
foreach($pagearray作为$PageU编号){
$uri=$dispencery.'?第='.$page\u no;
yield function()用法($uri){
返回$this->client->getAsync($uri);
};
}
}
}

下面是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();
}
函数postInBulk($inputs)
{
$client=新客户端([
'基本uri'=>'https://a.b.com'
]);
$headers=[
“授权”=>“来自\u directus\u用户的承载令牌”
];
$requests=函数($a)使用($client,$headers){
对于($i=0;$ipostAsync('https://a.com/project/items/collection', [
'headers'=>$headers,
“json”=>[
“代码段”=>“代码段”,
“排名”=>“1”,
“状态”=>“已发布”
]        
]);
};
}
};
$pool=新池($client,$requests,$input)[
“并发”=>5,
“已完成”=>函数(响应$Response,$index){
//每一次成功的响应都会传递此信息
},
“已拒绝”=>函数(RequestException$reason$index){
//这将在每个失败的请求中传递
},
]);
$pool->promise()->wait();
}
每个\u limit\u all()。