【发布时间】:2017-03-07 15:16:00
【问题描述】:
我认为下面的代码会这样工作:
- 发送
CONCURRENT_REQUESTS数量的批次 - 等待所有这些请求完成
- 上述号码的下一批已发送
- 等等
但实际上,如果我评论第 14 行 [usleep(...)],似乎请求批次会尽可能快地发送,从而向服务器生成数千个查询。
有可能改变它吗?如何改变这种行为?
<?php
$pool = $this->getPool();
if (false !== $pool) {
$pool->promise()->wait();
}
private function getPool()
{
$requests = function ($data) {
foreach ($data as $index => $datum) {
yield $this->patch($datum)->then(function (
$response
) use ($index) {
usleep(SLEEP_TIME_IN_SECONDS *1000000);
return [
'response' => $response,
'index' => $index
];
});
}
};
return new EachPromise($requests($data), [
'concurrency' => CONCURRENT_REQUESTS,
'fulfilled' => function ($response, $index) use ($data) {
// log
},
'rejected' => function ($reason, $index) use ($data) {
// do stuff
}
]);
}
private function patch($data)
{
$request = new Request(REQUEST_TYPE_PATCH, $url, $this->getPatchHeaders());
return $this->client->sendAsync($request);
}
【问题讨论】:
-
您确定使用 CurlMulti 处理程序吗?您是否安装并启用了 cURL?它不适用于 StreamHandler。
标签: php curl concurrency guzzle guzzle6