Php 在某些情况下,如何停止在Guzzle中发送异步请求?
当我捕捉到许多异常时,我希望停止在狂饮中发送请求。有人知道怎么做吗 下面是我的代码片段:Php 在某些情况下,如何停止在Guzzle中发送异步请求?,php,guzzle6,Php,Guzzle6,当我捕捉到许多异常时,我希望停止在狂饮中发送请求。有人知道怎么做吗 下面是我的代码片段: protected function parseAsyncCustomers($urls) { $promises = (function () use ($urls) { do { $uri = new Uri(current($urls)); $request = new Request('GET', $uri, ['User-Ag
protected function parseAsyncCustomers($urls)
{
$promises = (function () use ($urls) {
do {
$uri = new Uri(current($urls));
$request = new Request('GET', $uri, ['User-Agent' => UserAgent::random()]);
yield $this->httpClient->sendAsync($request, [
'timeout' => 15,
'connect_timeout' => 15,
]);
} while (next($urls) !== false);
})();
(new \GuzzleHttp\Promise\EachPromise($promises, [
// Multiple Concurrent HTTP Requests
'concurrency' => 10,
'fulfilled' => function (ResponseInterface $response, $index) {
$content = $response->getBody()->getContents();
$this->parseCustomerContent($content, $index);
},
'rejected' => function ($reason, $index) {
// This is delivered each failed request
if ($reason instanceof GuzzleException) {
if ($this->reject++ > 30) {
// how can stop sending next requests?
}
}
},
]))->promise()->wait();
}
rejected
回调还有第四个参数,它表示整个每个promise
。您可以在您的条件下拒绝它,它将停止执行流
'rejected' => function ($reason, $index, $idx, $aggregate) {
// This is delivered each failed request
if ($reason instanceof GuzzleException) {
if ($this->reject++ > 30) {
$aggregate->reject('Attempts limit exceeded')
}
}
},