我已经使用guzzle库发出了一些并发请求。完成此操作的代码:
public function concurrentRequests(array $uri)
{
$client = $this;
$requests = function ($total) use ($client, $uri) {
for ($i = 0; $i < $total; $i++) {
yield function () use ($client, $uri, $i) {
return $client->requestAsync('GET', $uri[$i]);
};
}
};
$pool = Pool::batch($client, $requests(count($uri)), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
// this is delivered each successful response
},
'rejected' => function ($reason, $index) {
// this is delivered each failed request
},
]);
return $pool;
}
我想知道哪个请求属于每个响应。遍历响应时有什么方法可以做到?
答案 0 :(得分:1)
这很容易,因为方法 Pool :: batch
以发送请求的顺序返回包含响应或异常的数组。