PHP多線程類及用法實(shí)例
本文實(shí)例講述了PHP多線程類及用法。分享給大家供大家參考。具體分析如下:
一般來(lái)說(shuō)可通過(guò)WEB服務(wù)器來(lái)實(shí)現(xiàn)PHP多線程功能,當(dāng)然,對(duì)多線程有深入理解的人都知道通過(guò)WEB服務(wù)器實(shí)現(xiàn)的多線程只能模仿多線程的一些效果,并不是真正意義上的多線程.
但不管怎么樣,它還是能滿足我們的一些需要的,在需要類似多線程的功能方面還是可以采用這個(gè)類,代碼如下:
* @title: PHP多線程類(Thread)
* @version: 1.0
*
* PHP多線程應(yīng)用示例:
* require_once 'thread.class.php';
* $thread = new thread();
* $thread->addthread('action_log','a');
* $thread->addthread('action_log','b');
* $thread->addthread('action_log','c');
* $thread->runthread();
*
* function action_log($info) {
* $log = 'log/' . microtime() . '.log';
* $txt = $info . "rnrn" . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn";
* $fp = fopen($log, 'w');
* fwrite($fp, $txt);
* fclose($fp);
* }
*/
class thread {
var $hooks = array();
var $args = array();
function thread() {
}
function addthread($func)
{
$args = array_slice(func_get_args(), 1);
$this->hooks[] = $func;
$this->args[] = $args;
return true;
}
function runthread()
{
if(isset($_GET['flag']))
{
$flag = intval($_GET['flag']);
}
if($flag || $flag === 0)
{
call_user_func_array($this->hooks[$flag], $this->args[$flag]);
}
else
{
for($i = 0, $size = count($this->hooks); $i < $size; $i++)
{
$fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);
if($fp)
{
$out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn";
$out .= "Host: {$_SERVER['HTTP_HOST']}rn";
$out .= "Connection: Closernrn";
fputs($fp,$out);
fclose($fp);
}
}
}
}
}
使用方法,代碼如下:
$thread->addthread('func1','info1');
$thread->addthread('func2','info2');
$thread->addthread('func3','info3');
$thread->runthread();
說(shuō)明:
addthread() 是添加線程函數(shù),第一個(gè)參數(shù)是函數(shù)名,之后的參數(shù)(可選)為傳遞給指定函數(shù)的參數(shù).
runthread() 是執(zhí)行線程的函數(shù).
PHP實(shí)例:利用curl實(shí)現(xiàn)多線程下載圖片
其實(shí)是php利用curl實(shí)現(xiàn)的一個(gè)多線程類,有了這個(gè)類,我們也可利用該類執(zhí)行多線程任務(wù)了,代碼如下:
private $url_list=array();
private $curl_setopt=array(
'CURLOPT_RETURNTRANSFER' => 1,//結(jié)果返回給變量
'CURLOPT_HEADER' => 0,//是否需要返回HTTP頭
'CURLOPT_NOBODY' => 0,//是否需要返回的內(nèi)容
'CURLOPT_FOLLOWLOCATION' => 0,//自動(dòng)跟蹤
'CURLOPT_TIMEOUT' => 6//超時(shí)時(shí)間(s)
);
function __construct($seconds=30){
set_time_limit($seconds);
}
/*
* 設(shè)置網(wǎng)址
* @list 數(shù)組
*/
public function setUrlList($list=array()){
$this->url_list=$list;
}
/*
* 設(shè)置參數(shù)
* @cutPot array
*/
public function setOpt($cutPot){
$this->curl_setopt=$cutPot+$this->curl_setopt;
}
/*
* 執(zhí)行
* @return array
*/
public function execute(){
$mh=curl_multi_init();
foreach($this->url_list as $i=>$url){
$conn[$i]=curl_init($url);
foreach($this->curl_setopt as $key => $val){
curl_setopt($conn[$i],preg_replace('/(CURLOPT_w{1,})/ie','$0',$key),$val);
}
curl_multi_add_handle($mh,$conn[$i]);
}
$active=false;
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);
}
}
$res=array();
foreach($this->url_list as $i => $url){
$res[$i]=curl_multi_getcontent($conn[$i]);
curl_close($conn[$i]);
curl_multi_remove_handle($mh,$conn[$i]);//釋放資源
}
curl_multi_close($mh);
return $res;
}
}
php使用多線程下載類示例,下載遠(yuǎn)程圖片,代碼如下:
$curl_mul->setUrlList(array('http://www.dbjr.com.cn/img/logo.jpg','http://www.dbjr.com.cn/img/logo.jpg','http://www.dbjr.com.cn/img/logo.jpg'));
$a=$curl_mul->execute();
$i=1;
foreach($a as $v){
$filename=$i.'.gif';
$fp2=@fopen($filename,'a');
fwrite($fp2,$v);
fclose($fp2);
$i++;
}
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- PHP pthreads v3使用中的一些坑和注意點(diǎn)分析
- php使用pthreads v3多線程實(shí)現(xiàn)抓取新浪新聞信息操作示例
- php pthreads多線程的安裝與使用
- 使用pthreads實(shí)現(xiàn)真正的PHP多線程(需PHP5.3以上版本)
- 實(shí)現(xiàn)PHP多線程異步請(qǐng)求的3種方法
- php三種實(shí)現(xiàn)多線程類似的方法
- php異步多線程swoole用法實(shí)例
- PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程
- PHP多線程抓取網(wǎng)頁(yè)實(shí)現(xiàn)代碼
- PHP多線程批量采集下載美女圖片的實(shí)現(xiàn)代碼(續(xù))
- PHP Curl多線程原理實(shí)例詳解
- PHP pthreads v3下的Volatile簡(jiǎn)介與使用方法示例
相關(guān)文章
php 微信開(kāi)發(fā)獲取用戶信息如何實(shí)現(xiàn)
這篇文章主要介紹了php 微信開(kāi)發(fā)獲取用戶信息如何實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-12-12php獲取四位字母和數(shù)字的隨機(jī)數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了php做程序開(kāi)發(fā)的過(guò)程中,我們很多時(shí)候會(huì)在登錄界面或者評(píng)論界面做一些四位數(shù)的驗(yàn)證碼,需要的朋友可以參考下2015-01-01PHP下利用shell后臺(tái)運(yùn)行PHP腳本,并獲取該腳本的Process ID的代碼
PHP下利用shell后臺(tái)運(yùn)行PHP腳本,并獲取該腳本的Process ID的代碼,學(xué)習(xí)php的朋友可以參考下。2011-09-09php實(shí)現(xiàn)基于openssl的加密解密方法
這篇文章主要介紹了php實(shí)現(xiàn)基于openssl的加密解密方法,結(jié)合實(shí)例形式分析了php自定義函數(shù)實(shí)現(xiàn)基于openssl的加密解密操作相關(guān)技巧,需要的朋友可以參考下2016-09-09PHP數(shù)學(xué)運(yùn)算函數(shù)大匯總(經(jīng)典值得收藏)
這篇文章主要介紹了PHP數(shù)學(xué)運(yùn)算函數(shù),匯總分析了常見(jiàn)的PHP數(shù)學(xué)運(yùn)算函數(shù)的功能,使用方法與注意事項(xiàng),需要的朋友可以參考下2016-04-04php實(shí)現(xiàn)數(shù)組中索引關(guān)聯(lián)數(shù)據(jù)轉(zhuǎn)換成json對(duì)象的方法
這篇文章主要介紹了php實(shí)現(xiàn)數(shù)組中索引關(guān)聯(lián)數(shù)據(jù)轉(zhuǎn)換成json對(duì)象的方法,基于Yii框架分析了php數(shù)組與json格式數(shù)據(jù)的轉(zhuǎn)換技巧,需要的朋友可以參考下2015-07-07