我這裡寫了個簡單的curl採集,但是執行後發現單線程執行的方式比多線程執行要快很多.
是我的寫法又問題嗎?
$images = [ "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415151923_73502.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415151826_52170.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415152035_59698.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140507/20140507143708_26688.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140417/20140417095153_61993.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140426/20140426094716_96396.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20130730/20130730160625_21437.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20130731/20130731170502_90104.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20130731/20130731165147_80414.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415151923_73502.jpg",];
這個是單線程的函數:
function getImg($url = "", $filename = ""){ $ch = curl_init (); $opt [CURLOPT_URL] = $url; $opt [CURLOPT_HEADER] = true; $opt [CURLOPT_CONNECTTIMEOUT] = 10; $opt [CURLOPT_TIMEOUT] = 60; $opt [CURLOPT_AUTOREFERER] = true; $opt [CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11'; $opt [CURLOPT_RETURNTRANSFER] = true; // $opt [CURLOPT_FOLLOWLOCATION] = true; //跟隨重新導向 // $opt [CURLOPT_MAXREDIRS] = 10; curl_setopt_array ( $ch, $opt ); $r = curl_exec ( $ch ); if (false === $r) { $errno = curl_errno ( $ch ); $err = curl_error ( $ch ); curl_close ( $ch ); return false; } // 評鑑一下header:200 才寫入檔案 $header = explode("\r\n\r\n", $r); if (strpos($header[0], 'HTTP/1.1 200') === 0) { file_put_contents($filename, $header[1]); } curl_close ( $ch ); return true;}
又嘗試用curl_multi系列函數,但直接看的手冊,沒完全弄明白:
// 多線程採集資料function getImgMulti($url=[] , $filename=[]){ // 建立批處理cURL控制代碼 $mh = curl_multi_init(); // 這裡可以加n=10個線程 foreach ($url as $k => $v ) { $ch[$k] = curl_init(); $opt [CURLOPT_URL] = $v; $opt [CURLOPT_HEADER] = true; $opt [CURLOPT_CONNECTTIMEOUT] = 10; $opt [CURLOPT_TIMEOUT] = 60; $opt [CURLOPT_AUTOREFERER] = true; $opt [CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11'; $opt [CURLOPT_RETURNTRANSFER] = true; // $opt [CURLOPT_FOLLOWLOCATION] = true; //跟隨重新導向 // $opt [CURLOPT_MAXREDIRS] = 10; curl_setopt_array ( $ch[$k], $opt ); // 增加1個控制代碼 curl_multi_add_handle($mh,$ch[$k]); } $running=null; // 執行批處理控制代碼 do { curl_multi_exec($mh,$running); } while($running > 0); for ($i=0; $i < 10; $i++) { $r = curl_multi_getcontent($ch[$i]); // 評鑑一下header:200 才寫入檔案 $header = explode("\r\n\r\n", $r); if (strpos($header[0], 'HTTP/1.1 200') === 0) { file_put_contents('pics/'.$i.'.jpg', $header[1]); } } // 關閉全部控制代碼 // curl_multi_remove_handle($mh, $ch1); curl_multi_close($mh);}
執行結果,迴圈執行單線程大約1.7秒完成,後面這個3.5秒完成.
可能是我對這個函數的用法沒弄透,請哪位來解釋下原因?
----後續補充----
我在windows上面測試的,難道說因為win的php多線程有別的問題?
另外還參考了別人寫好的php類
http://blog.eiodesign.com/archives/86
用這個類庫又做了一遍採集,結果還是一樣,更慢了
// 測試庫採集require("libs/class_curl_multi.php");$mp = new MultiHttpRequest();//遠程圖片本地化$mp->set_urls($images);$images_result = $mp->start();foreach ((array)$images_result as $image_key => $image_value) { if (!empty($image_key)) { _flush("store image:".$image_key."
"); file_put_contents('pics/'.$image_key.'.jpg',$image_value); }}
用時4.05秒
是因為我對這個php的多線程理解有問題,還是其他原因造成這種差距呢? 貌似多線程並沒有提高採集效率.反而影響了.
回複內容:
我這裡寫了個簡單的curl採集,但是執行後發現單線程執行的方式比多線程執行要快很多.
是我的寫法又問題嗎?
$images = [ "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415151923_73502.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415151826_52170.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415152035_59698.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140507/20140507143708_26688.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140417/20140417095153_61993.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140426/20140426094716_96396.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20130730/20130730160625_21437.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20130731/20130731170502_90104.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20130731/20130731165147_80414.jpg", "http://pic.91taojin.com.cn/data/attachment/image/20140415/20140415151923_73502.jpg",];
這個是單線程的函數:
function getImg($url = "", $filename = ""){ $ch = curl_init (); $opt [CURLOPT_URL] = $url; $opt [CURLOPT_HEADER] = true; $opt [CURLOPT_CONNECTTIMEOUT] = 10; $opt [CURLOPT_TIMEOUT] = 60; $opt [CURLOPT_AUTOREFERER] = true; $opt [CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11'; $opt [CURLOPT_RETURNTRANSFER] = true; // $opt [CURLOPT_FOLLOWLOCATION] = true; //跟隨重新導向 // $opt [CURLOPT_MAXREDIRS] = 10; curl_setopt_array ( $ch, $opt ); $r = curl_exec ( $ch ); if (false === $r) { $errno = curl_errno ( $ch ); $err = curl_error ( $ch ); curl_close ( $ch ); return false; } // 評鑑一下header:200 才寫入檔案 $header = explode("\r\n\r\n", $r); if (strpos($header[0], 'HTTP/1.1 200') === 0) { file_put_contents($filename, $header[1]); } curl_close ( $ch ); return true;}
又嘗試用curl_multi系列函數,但直接看的手冊,沒完全弄明白:
// 多線程採集資料function getImgMulti($url=[] , $filename=[]){ // 建立批處理cURL控制代碼 $mh = curl_multi_init(); // 這裡可以加n=10個線程 foreach ($url as $k => $v ) { $ch[$k] = curl_init(); $opt [CURLOPT_URL] = $v; $opt [CURLOPT_HEADER] = true; $opt [CURLOPT_CONNECTTIMEOUT] = 10; $opt [CURLOPT_TIMEOUT] = 60; $opt [CURLOPT_AUTOREFERER] = true; $opt [CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11'; $opt [CURLOPT_RETURNTRANSFER] = true; // $opt [CURLOPT_FOLLOWLOCATION] = true; //跟隨重新導向 // $opt [CURLOPT_MAXREDIRS] = 10; curl_setopt_array ( $ch[$k], $opt ); // 增加1個控制代碼 curl_multi_add_handle($mh,$ch[$k]); } $running=null; // 執行批處理控制代碼 do { curl_multi_exec($mh,$running); } while($running > 0); for ($i=0; $i < 10; $i++) { $r = curl_multi_getcontent($ch[$i]); // 評鑑一下header:200 才寫入檔案 $header = explode("\r\n\r\n", $r); if (strpos($header[0], 'HTTP/1.1 200') === 0) { file_put_contents('pics/'.$i.'.jpg', $header[1]); } } // 關閉全部控制代碼 // curl_multi_remove_handle($mh, $ch1); curl_multi_close($mh);}
執行結果,迴圈執行單線程大約1.7秒完成,後面這個3.5秒完成.
可能是我對這個函數的用法沒弄透,請哪位來解釋下原因?
----後續補充----
我在windows上面測試的,難道說因為win的php多線程有別的問題?
另外還參考了別人寫好的php類
http://blog.eiodesign.com/archives/86
用這個類庫又做了一遍採集,結果還是一樣,更慢了
// 測試庫採集require("libs/class_curl_multi.php");$mp = new MultiHttpRequest();//遠程圖片本地化$mp->set_urls($images);$images_result = $mp->start();foreach ((array)$images_result as $image_key => $image_value) { if (!empty($image_key)) { _flush("store image:".$image_key."
"); file_put_contents('pics/'.$image_key.'.jpg',$image_value); }}
用時4.05秒
是因為我對這個php的多線程理解有問題,還是其他原因造成這種差距呢? 貌似多線程並沒有提高採集效率.反而影響了.