GuzzleHttp 并发异步请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$client = new Client();

$requests = function ($total) {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
yield new Request('GET', $uri);
}
};

$pool = new Pool($client, $requests(100), [
'concurrency' => 5,
'fulfilled' => function (Response $response, $index) {
// 这里写成功的逻辑
},
'rejected' => function (RequestException $reason, $index) {
// 这里写异常的逻辑
},
]);

$promise = $pool->promise();
$promise->wait();