如何将某些元数据添加到Pool中发送的大量请求中?我需要为请求添加ID,以确定将哪个请求分配给响应。我想要这样的东西-将id设置为请求并从响应中获取它。将id设置为Header似乎不是一个好主意。请帮忙。
$requests = function ($total) {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
$myAdditionalId = $i;
yield new Request('GET', $uri, $myAdditionalId);
}
};
$pool = new Pool($client, $requests(100), [
'concurrency' => 5,
'fulfilled' => function (Response $response, $index) {
echo $response->getMyAdditionalId();
// this is delivered each successful response
},
'rejected' => function (RequestException $reason, $index) {
// this is delivered each failed request
},
]);
我以这种方式解决了这个问题:
$requests = function ($total) {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
$myAdditionalId = $i;
$promise = $client->getAsync($uri);
$promise->then(function () use ($myAdditionalId){
//add callback with function on success
echo $myAdditionalId;
});
yield function()use($promise){return $promise;};
}
};
$pool = new Pool($client, $requests(100), [
'concurrency' => 5,
'rejected' => function (RequestException $reason, $index) {
// this is delivered each failed request
},
]);