为什么我这段curl采集,单线程比多线程还快?
我这里写了个简单的curl采集,但是执行后发现单线程执行的方式比多线程执行要快很多.
是我的写法又问题吗?
<code>$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", ]; </code>
这个是单线程的函数:
<code>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; } </code>
又尝试用curl_multi系列函数,但直接看的手册,没完全弄明白:
<code>// 多线程采集数据 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 </code>
执行结果,循环执行单线程大约1.7秒完成,后面这个3.5秒完成.
可能是我对这个函数的用法没弄透,请哪位来解释下原因?
----后续补充----
我在windows上面测试的,难道说因为win的php多线程有别的问题?
另外还参考了别人写好的php类
http://blog.eiodesign.com/archives/86
用这个类库又做了一遍采集,结果还是一样,更慢了
<code>// 测试库采集 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."<br>"); file_put_contents('pics/'.$image_key.'.jpg',$image_value); } } </code>
用时4.05秒
是因为我对这个php的多线程理解有问题,还是其他原因造成这种差距呢? 貌似多线程并没有提高采集效率.反而影响了.
回复内容:
我这里写了个简单的curl采集,但是执行后发现单线程执行的方式比多线程执行要快很多.
是我的写法又问题吗?
<code>$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", ]; </code>
这个是单线程的函数:
<code>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; } </code>
又尝试用curl_multi系列函数,但直接看的手册,没完全弄明白:
<code>// 多线程采集数据 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 </code>
执行结果,循环执行单线程大约1.7秒完成,后面这个3.5秒完成.
可能是我对这个函数的用法没弄透,请哪位来解释下原因?
----后续补充----
我在windows上面测试的,难道说因为win的php多线程有别的问题?
另外还参考了别人写好的php类
http://blog.eiodesign.com/archives/86
用这个类库又做了一遍采集,结果还是一样,更慢了
<code>// 测试库采集 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."<br>"); file_put_contents('pics/'.$image_key.'.jpg',$image_value); } } </code>
用时4.05秒
是因为我对这个php的多线程理解有问题,还是其他原因造成这种差距呢? 貌似多线程并没有提高采集效率.反而影响了.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는
