2014年11月14日 星期五
PHP 使用 CURL 同步抓取多個網頁
一般 CURL 抓網頁的方法, 是一頁一頁抓, 假設要抓 4頁, 所費時間各別是 5,10,7,5 秒, 那全部總合所花的時間就是 5 + 10 + 7 + 5 = 27 秒.
若能同時間去抓取多個網頁, 所花費的時間 5,10,7,5 秒, 全部總合所花的時間是 10 秒.(花費最多時間的秒數)
於 JavaScript 可使用 AJAX 的 async(YAHOO.util.Connect.asyncRequest) 來達成, 於 PHP 可以用 CURL 來達成此 Multi-Threading 的效果.
官方文件: PHP: curl_multi_init
程式(async.php)
View Raw Code?
0) // 每個 connect 要間隔多久
usleep($wait_usec); // 250000 = 0.25 sec
} while ($running > 0);
*/
/* 此做法就可以避免掉 CPU loading 100% 的問題 */
// 參考自: http://www.hengss.com/xueyuan/sort0362/php/info-36963.html
/* 此作法可能會發生無窮迴圈
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active and $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
*/
/*
// 感謝 Ren 指點的作法. (需要在測試一下)
// curl_multi_exec的返回值是用來返回多線程處裡時的錯誤,正常來說返回值是0,也就是說只用$mrc捕捉返回值當成判斷式的迴圈只會運行一次,而真的發生錯誤時,有拿$mrc判斷的都會變死迴圈。
// 而curl_multi_select的功能是curl發送請求後,在有回應前會一直處於等待狀態,所以不需要把它導入空迴圈,它就像是會自己做判斷&自己決定等待時間的sleep()。
*/
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
/* 讀取資料 */
foreach($handle as $i => $ch) {
$content = curl_multi_getcontent($ch);
$data[$i] = (curl_errno($ch) == 0) ? $content : false;
}
/* 移除 handle*/
foreach($handle as $ch) {
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
return $data;
}
?>
使用
View Raw Code?
example1, [1] => example2
?>
測試
sleep.php # 看時間延長取得的效果
View Raw Code?
View Raw Code?
5, [1] => 10, [2] => 7, [3] => 5
?>
http://blog.longwin.com.tw/2009/10/php-multi-thread-curl-2009/
2014年11月13日 星期四
Resolving Stubborn Wi-Fi Connection Problems in Mac OS X
A fair amount of Mac users have encountered long lasting Wi-Fi connection issues, whether it’s a dropping connection, a refusal to reconnect after waking from sleep, or any other number of oddities pertaining to wireless networking. Often these connection problems can be resolved with renewing a DHCP lease and changing the MTU size, but sometimes things are more stubborn and a wireless connection will continue to drop or throw unusual connection errors when waking from sleep. If you find yourself struggling with persistent wifi problems, try deleting all preference plist files associated with wireless settings in OS X:
Fix Stubborn Wi-Fi Problems by Deleting Preference Files
Before beginning, be sure to have the wi-fi password of your primary network handy. You will need it to rejoin the network.
- Pull down the Wi-Fi menu and turn Wi-FI OFF
- From any Finder window, hit Command+Shift+G to bring up Go To Folder, and enter the following path:
- Locate the following files, copy them to the desktop for a backup, then delete them from the /SystemConfiguration/ folder:
- Empty the Trash and reboot the Mac
- Turn Wi-Fi back ON from the Wi-Fi menu, join your preferred network as usual
/Library/Preferences/SystemConfiguration/
com.apple.airport.preferences.plist
com.apple.network.identification.plist
NetworkInterfaces.plist
preferences.plist
This process forces OS X to create all new preference files for wireless networking, which can be an effective troubleshooting strategy if the wi-fi problems began after upgrading versions of Mac OS X and even installing incremental system updates.
The wi-fi connection should now work as expected unless there’s a deeper problem, whether it’s a compatibility issue with the router (often resolved by this DHCP and MTU fix), a problem the router itself, or something as simple as network interference (here’s how to check connection strength and interference issues), which can often be resolved by reconfiguring a routers physical arrangement or changing it’s broadcast channel.
Let us know if this worked for you, or if you have any other helpful wifi troubleshooting tips. reference : http://osxdaily.com/2012/11/30/resolving-stubborn-wi-fi-connection-problems-in-mac-os-x/
2014年11月11日 星期二
訂閱:
文章 (Atom)