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

首頁 > 開發 > PHP > 正文

PHP 多線程的實現(PHP多線程類)

2024-05-04 23:24:17
字體:
來源:轉載
供稿:網友

通過WEB服務器來實現PHP多線程功能。

當然,對多線程有深入理解的人都知道通過WEB服務器實現的多線程只能模仿多線程的一些效果,并不是真正意義上的多線程。

但不管怎么樣,它還是能滿足我們的一些需要的,在需要類似多線程的功能方面還是可以采用這個類。

/** * @title:		PHP多線程類(Thread) * @version:	1.0 * @author:		phper.org.cn < web@phper.org.cn > * @published:	2010-11-2 *  * PHP多線程應用示例: *  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 . "/r/n/r/n" . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "/r/n"; *  	$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.1/r/n";        			$out .= "Host: {$_SERVER['HTTP_HOST']}/r/n";        			$out .= "Connection: Close/r/n/r/n";	                fputs($fp,$out);	                fclose($fp);        		}        	}    	}    }}

使用方法:

$thread = new thread();
$thread->addthread('func1','info1');
$thread->addthread('func2','info2');
$thread->addthread('func3','info3');
$thread->runthread();

說明:

addthread是添加線程函數,第一個參數是函數名,之后的參數(可選)為傳遞給指定函數的參數。

runthread是執行線程的函數。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛浦县| 静安区| 桃江县| 南宁市| 彰化县| 阜南县| 留坝县| 安多县| 汶川县| 木里| 贵定县| 新竹县| 兴城市| 开鲁县| 大厂| 郴州市| 长岭县| 雅江县| 和政县| 宁河县| 宜丰县| 敦煌市| 秦皇岛市| 大方县| 四川省| 惠州市| 无锡市| 翁源县| 团风县| 新乡县| 依兰县| 峨边| 枣阳市| 通河县| 汕尾市| 大宁县| 且末县| 曲周县| 河曲县| 诸城市| 九龙县|