国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP多線程類及用法實(shí)例

2020-03-22 18:44:24
字體:
供稿:網(wǎng)友
本文實(shí)例講述了PHP多線程類及用法。分享給大家供大家參考。具體分析如下:一般來說可通過WEB服務(wù)器來實(shí)現(xiàn)PHP多線程功能,當(dāng)然,對多線程有深入理解的人都知道通過WEB服務(wù)器實(shí)現(xiàn)的多線程只能模仿多線程的一些效果,并不是真正意義上的多線程.但不管怎么樣,它還是能滿足我們的一些需要的,在需要類似多線程的功能方面還是可以采用這個(gè)類,代碼如下:
復(fù)制代碼 代碼如下:/**
* @title: PHP多線程類(Thread)
* @version: 1.0
*
* PHP多線程應(yīng)用示例:
* require_once 'thread.html' target='_blank'>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);
}
}
}
}
}
使用方法,代碼如下:
復(fù)制代碼 代碼如下:$thread = new thread();
$thread- addthread('func1','info1');
$thread- addthread('func2','info2');
$thread- addthread('func3','info3');
$thread- runthread();
說明: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ù)了,代碼如下:
復(fù)制代碼 代碼如下:class curl_multi{
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)程圖片,代碼如下:
復(fù)制代碼 代碼如下:$curl_mul=new curl_multi();
$curl_mul- setUrlList(array('http://www.phpstudy.net/img/logo.jpg','http://www.phpstudy.net/img/logo.jpg','http://www.phpstudy.net/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++;
}希望本文所述對大家的PHP程序設(shè)計(jì)有所幫助。PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 息烽县| 隆昌县| 墨脱县| 吉首市| 上高县| 成都市| 中西区| 依兰县| 滦南县| 社旗县| 乃东县| 巩留县| 萨迦县| 望奎县| 遂平县| 化德县| 招远市| 桐柏县| 衡南县| 通许县| 东安县| 青川县| 锡林浩特市| 巧家县| 万年县| 韶关市| 高清| 汝州市| 潼南县| 彩票| 娱乐| 静海县| 云龙县| 徐汇区| 渝中区| 海晏县| 抚州市| 灵川县| 承德市| 彭泽县| 大渡口区|