让你的PHP程序真正的实现多线程(PHP多线程类)(转)

本文介绍了一个用于在PHP环境中实现多线程功能的类。通过此类,开发者可以轻松地添加并运行多个线程,每个线程执行特定的操作,如日志记录。示例包括类的定义、添加线程的方法以及执行线程的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过WEB服务器来实现PHP多线程功能。

当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程。

但不管怎么样,它还是能满足我们的一些需要的,在需要类似多线程的功能方面还是可以采用这个类。

 

 1     /**   
 2      * @title:      PHP多线程类(Thread)   
 3      * @version:    1.0   
 4      * @author:     phper.org.cn < web@phper.org.cn >   
 5      * @published:  2010-11-2   
 6      *    
 7      * PHP多线程应用示例:   
 8      *  require_once 'thread.class.php';   
 9      *  $thread = new thread();   
10      *  $thread->addthread('action_log','a');   
11      *  $thread->addthread('action_log','b');   
12      *  $thread->addthread('action_log','c');   
13      *  $thread->runthread();   
14      *     
15      *  function action_log($info) {   
16      *      $log = 'log/' . microtime() . '.log';   
17      *      $txt = $info . " " . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . " ";   
18      *      $fp = fopen($log, 'w');   
19      *      fwrite($fp, $txt);   
20      *      fclose($fp);   
21      *  }   
22      */  
23     class thread {    
24              
25         var $hooks = array();    
26         var $args = array();    
27              
28         function thread() {    
29         }    
30              
31         function addthread($func)    
32         {    
33             $args = array_slice(func_get_args(), 1);    
34             $this->hooks[] = $func;    
35             $this->args[] = $args;    
36             return true;    
37         }    
38              
39         function runthread()    
40         {    
41             if(isset($_GET['flag']))    
42             {    
43                 $flag = intval($_GET['flag']);    
44             }    
45             if($flag || $flag === 0)    
46             {    
47                 call_user_func_array($this->hooks[$flag], $this->args[$flag]);    
48             }    
49             else    
50             {    
51                 for($i = 0, $size = count($this->hooks); $i < $size; $i++)    
52                 {    
53                     $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);    
54                     if($fp)    
55                     {    
56                         $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1 ";    
57                         $out .= "Host: {$_SERVER['HTTP_HOST']} ";    
58                         $out .= "Connection: Close ";    
59                         fputs($fp,$out);    
60                         fclose($fp);    
61                     }    
62                 }    
63             }    
64         }    
65     }  

 

使用方法:

1     $thread = new thread();    
2     $thread->addthread('func1','info1');    
3     $thread->addthread('func2','info2');    
4     $thread->addthread('func3','info3');    
5     $thread->runthread();  

说明:

addthread是添加线程函数,第一个参数是函数名,之后的参数(可选)为传递给指定函数的参数。

runthread是执行线程的函数。

 

转载于:https://www.cnblogs.com/xingmeng/p/3544419.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值