[英]PHP Multiple cURL requests to REST API stalls
Currently I have a system that sends multiple requests to a REST API. 目前我有一个系统可以向REST API发送多个请求。 It is structured something like this:
它的结构类似于:
foreach ($data as $d)
{
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_HTTPHEADER, (array of data here));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
$retry = 0;
while((curl_errno($ch) == 7 || curl_errno($ch) == 52) && $retry < 3)
{
$response = curl_exec($ch);
$retry++;
}
curl_close($ch);
(decode XML Response and loop)
}
(I can't expose the whole code so I have filled in the operations that are happening in brackes) (我无法公开整个代码,所以我填写了在括号中发生的操作)
However after a few hundred requests the FastCGI script stalls. 但是在几百次请求之后,FastCGI脚本停止了。 The REST API will still respond during this period if I query it in another fashion, but this batch client will not send anymore requests.
如果我以其他方式查询REST API,REST API仍将在此期间响应,但此批处理客户端将不再发送请求。 After a few minutes, it will start responding again.
几分钟后,它将再次开始响应。 I'm not sure why this is stalling, I can see via htop that there is no CPU activity on the threads at either end whilst this is happening.
我不知道为什么这会停滞不前,我可以通过htop看到两端的线程都没有CPU活动,而这种情况正在发生。
Is there any reason why the cURL/PHP script would stall here? 有没有理由说cURL / PHP脚本会在这里停止?
if you allowed to use external PHP libraries; 如果你允许使用外部PHP库; I'd like to suggest this method: https://github.com/php-curl-class/php-curl-class
我想建议这个方法: https : //github.com/php-curl-class/php-curl-class
// Requests in parallel with callback functions.
$multi_curl = new MultiCurl();
$multi_curl->success(function($instance) {
echo 'call to "' . $instance->url . '" was successful.' . "\n";
echo 'response: ' . $instance->response . "\n";
});
$multi_curl->error(function($instance) {
echo 'call to "' . $instance->url . '" was unsuccessful.' . "\n";
echo 'error code: ' . $instance->error_code . "\n";
echo 'error message: ' . $instance->error_message . "\n";
});
$multi_curl->complete(function($instance) {
echo 'call completed' . "\n";
});
$multi_curl->addGet('https://www.google.com/search', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://duckduckgo.com/', array(
'q' => 'hello world',
));
$multi_curl->addGet('https://www.bing.com/search', array(
'q' => 'hello world',
));
$multi_curl->start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.