博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP多线程的实现(PHP多线程类)
阅读量:4640 次
发布时间:2019-06-09

本文共 2100 字,大约阅读时间需要 7 分钟。

通过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是执行线程的函数。

转载于:https://www.cnblogs.com/yzh5251/p/3699823.html

你可能感兴趣的文章
[洛谷P3292][SCOI2016]幸运数字
查看>>
淘宝网质量分析
查看>>
B - Catch That Cow
查看>>
CSS Sprite、CSS雪碧图应用实例
查看>>
并发总结(博客转载)
查看>>
win 使用 tensorboard
查看>>
C#string类型总结
查看>>
shell下office、html、pdf文档互转方法
查看>>
浏览器事件window.onload、o…
查看>>
webpack学习笔记--配置module
查看>>
11.使用Document方法
查看>>
第十七章 springboot + devtools(热部署)
查看>>
MyBatis Spring MapperScannerConfigurer 配置
查看>>
学习v-on的使用
查看>>
Arduino下读取DHT22温湿度(不使用第三方库)
查看>>
最长不重复子串
查看>>
asp.net 逻辑操作符与(&&),或(||),非(!)
查看>>
Linux的进程/线程通信方式总结(转)
查看>>
一个java程序员的C++学习之路(整理)
查看>>
10个出色的NoSQL数据库
查看>>