Home  >  Article  >  php教程  >  ThinkPHP进程计数类Process用法实例详解

ThinkPHP进程计数类Process用法实例详解

WBOY
WBOYOriginal
2016-06-06 19:42:401183browse

这篇文章主要介绍了ThinkPHP进程计数类Process用法,以实例形式较为详细的分析了Process类的定义及进程计数的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考

本文实例讲述了ThinkPHP进程计数类Process用法。分享给大家供大家参考。具体如下:

项目中有一个需求:由于某一后台任务比较占带宽,所以要限制进程数。花了点时间,,写了类,目前版本功能比较简单。

Process.class.php文件如下:

* @license PHP Version 3.0 {@link } */ class Process { const PROCESS_KEY = '~Process'; const PROCESS_MAXNUM = 10; /** * start * * @static * @access public * @return void */ static public function start(){ $list = self::__getList(); $name = self::__getName(); if(!isset($list[$name])){ $list[$name] = array('count'=>1, 'lasttime'=>time()); }else{ if((time()-$list[$name]['time']) > 600){ $list[$name]['count'] = 1; }else{ $list[$name]['count'] += 1; } } self::__setList($list); } /** * destory * * @static * @access public * @return void */ static public function destory(){ $list = self::__getList(); $name = self::__getName(); if(isset($list[$name])){ if($list[$name]['count'] self::getMaxnum()); } /** * getLasttime * * @static * @access public * @return void */ static public function getLasttime(){ $list = self::__getList(); $name = self::__getName(); return $list[$name]['lasttime']; } /** * clear * * @static * @access public * @return void */ static public function clear(){ F(self::PROCESS_KEY, null); } /** * __setList * * @param mixed $list * @static * @access private * @return void */ static private function __setList($list=null){ if(!is_array($list) || empty($list)) F(self::PROCESS_KEY, null); else F(self::PROCESS_KEY, $list); } /** * __getList * * @static * @access private * @return void */ static private function __getList(){ $list = F(self::PROCESS_KEY); if(!is_array($list)) return array(); else return $list; } /** * __getName * * @static * @access private * @return void */ static private function __getName(){ return (defined('GROUP_NAME') ? GROUP_NAME.'_' : '') . MODULE_NAME . '_' . ACTION_NAME; } } ?>

调用方法:

希望本文所述对大家基于ThinkPHP框架的php程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn