phpcurlバッチ処理制御による同時実行性と非同期

WBOY
リリース: 2016-06-13 13:11:12
オリジナル
1195 人が閲覧しました

PHP CURL バッチ処理 -- 制御可能な同時実行性と非同期
PHP マニュアルには次のコードがあります:

$mrc = curl_multi_init();
//发出请求
.......
$active = null;
		do {
		    $mrc = curl_multi_exec($mh, $active);
		} while ($mrc == CURLM_CALL_MULTI_PERFORM);
		
		while ($active && $mrc == CURLM_OK) {
		    if (curl_multi_select($mh) != -1) {
		        do {
		            $mrc = curl_multi_exec($mh, $active);
		        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
		    }
		}
//下面是处理请求返回的结果
ログイン後にコピー


この do...while は、$mrc に対するすべてのリクエストを完了してから実行するために使用されます。応答結果

しかし、リクエストが 1000 件ある場合、curl バッチ処理は同時に 1000 件のリクエストを行うことになります。これは明らかに不合理であるため、同時実行数を制御し、残りの接続をリクエスト キューに追加する必要があります。
参考: http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/

  $mh = curl_multi_init();
        $ch = array();
        $chunck = 10; //并发控制数
        $all = count($urls);//所有的请求url数组
        $chunck = $all > $chunck ? $chunck : $all;
		
		$options = array(
			CURLOPT_HEADER=>FALSE,
			CURLOPT_RETURNTRANSFER=>TRUE,
			CURLOPT_FOLLOWLOCATION=>TRUE,
			CURLOPT_MAXREDIRS=>5,
			CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20100101 Firefox/6.0'
		);
		
		for($i = 0 ; $i < $chunck ; $i++){
			$ch[$i] = curl_init();
			curl_setopt($ch[$i],CURLOPT_URL,$urls[$i]);
			curl_setopt_array($ch[$i],$options);
			curl_multi_add_handle($mh,$ch[$i]);
		}
		
		do {
	        while(($execrun = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM);
	        if($execrun != CURLM_OK)break;
	        // a request was just completed -- find out which one
	        while($done = curl_multi_info_read($mh)) {
                //获取已经返回的url在urls数组里德的index
	            $index = array_search($done['handle'],$ch);
	            
	            $info = curl_getinfo($done['handle']);
	            
	            if ($info['http_code'] == 200){
	                $output = curl_multi_getcontent($ch[$index]);
	                // request successful.  process output using the callback function.
	                $save_path = $i.'.txt';//数据保存路径
					file_put_contents($save_path,$output);
	
	                // start a new request (it's important to do this before removing the old one)
	                $next = $i++;// increment i
	                $ch[$next] = curl_init();
	                $options[CURLOPT_URL] = $urls[$next];//将下一个请求添加到队列 
	                curl_setopt_array($ch[$next],$options);
	                curl_multi_add_handle($mh, $ch[$next]);
	
	                // remove the curl handle that just completed
	                curl_multi_remove_handle($mh, $done['handle']);
	            } else {
	                // request failed.  add error handling.
	            }
	        }
    	} while ($running);
		
		curl_multi_close($mh);
    
ログイン後にコピー


非常にうまく機能します使用後は副作用がなく、同時実行数を制御でき、さまざまな用途に使用できます。
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!