目前我有一个系统可以向REST API发送多个请求.它的结构类似于:
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) }
(我无法公开整个代码,所以我填写了在括号中发生的操作)
但是在几百次请求之后,FastCGI脚本停止了.如果我以其他方式查询REST API,REST API仍将在此期间响应,但此批处理客户端将不再发送请求.几分钟后,它将再次开始响应.我不知道为什么这会停滞不前,我可以通过htop看到两端的线程都没有cpu活动,而这种情况正在发生.
解决方法
如果你允许使用外部PHP库;
我想建议这个方法:
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/',)); $multi_curl->addGet('https://www.bing.com/search',)); $multi_curl->start();