使用 Guzzle 6 并发调用并跟踪额外参数
Use Guzzle 6 Concurrent call and keep track of extra parameters
所以我使用 Guzzle 6 进行不确定的并发 api 调用,但我想做的一件事是跟踪 promise 当前正在处理的数组值,因为我最初处理 api 根据数据库查询结果调用。之后,我想使用从 api.
返回的任何内容将值更新回数据库
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$requests = function () {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
foreach($database_result as $res) {
/*the res array contains
['id' => 'db id', 'query' => 'get query array'];
*/
$url = $uri . '?' . http_build_query($res['query']);
yield new Request('GET', $url);
}
};
$pool = new Pool($client, $requests(), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
/**
* HERE i want to be able to somehow
* retrieve the current responses db id
* this way I can obviously update anything
* i want on the db side
*/
},
'rejected' => function ($reason, $index) {
/**
* HERE i want to be able to somehow
* retrieve the current responses db id
* this way I can obviously update anything
* i want on the db side
*/
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
...
任何对此的帮助都会很棒。我想获得有关如何最好地处理这种情况的建议。我更愿意以一种聪明、合乎逻辑的方式来做。
感谢您的帮助
所以我想通了。
基本上
$requests = function () {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
foreach($database_result as $key => $res) {
/*the res array was updated to be
['id' => 'get query array'];
*/
$url = $uri . '?' . http_build_query($res);
//here is the key difference in change
yield $key => new Request('GET', $url);
}
};
稍后池功能中的索引将包含您想要的索引。
希望这对您有所帮助。
所以我使用 Guzzle 6 进行不确定的并发 api 调用,但我想做的一件事是跟踪 promise 当前正在处理的数组值,因为我最初处理 api 根据数据库查询结果调用。之后,我想使用从 api.
返回的任何内容将值更新回数据库use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$requests = function () {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
foreach($database_result as $res) {
/*the res array contains
['id' => 'db id', 'query' => 'get query array'];
*/
$url = $uri . '?' . http_build_query($res['query']);
yield new Request('GET', $url);
}
};
$pool = new Pool($client, $requests(), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
/**
* HERE i want to be able to somehow
* retrieve the current responses db id
* this way I can obviously update anything
* i want on the db side
*/
},
'rejected' => function ($reason, $index) {
/**
* HERE i want to be able to somehow
* retrieve the current responses db id
* this way I can obviously update anything
* i want on the db side
*/
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
...
任何对此的帮助都会很棒。我想获得有关如何最好地处理这种情况的建议。我更愿意以一种聪明、合乎逻辑的方式来做。
感谢您的帮助
所以我想通了。
基本上
$requests = function () {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
foreach($database_result as $key => $res) {
/*the res array was updated to be
['id' => 'get query array'];
*/
$url = $uri . '?' . http_build_query($res);
//here is the key difference in change
yield $key => new Request('GET', $url);
}
};
稍后池功能中的索引将包含您想要的索引。
希望这对您有所帮助。