ThinkPHP进程计数类Process用法实例详解
程序员文章站
2022-05-03 16:23:43
本文实例讲述了thinkphp进程计数类process用法。分享给大家供大家参考。具体如下:
项目中有一个需求:由于某一后台任务比较占带宽,所以要限制进程数。花了点时间,...
本文实例讲述了thinkphp进程计数类process用法。分享给大家供大家参考。具体如下:
项目中有一个需求:由于某一后台任务比较占带宽,所以要限制进程数。花了点时间,写了类,目前版本功能比较简单。
process.class.php文件如下:
<?php /** * process * * @package * @version $id$ * @copyright 2005-2011 sucop.com * @author dijia huang <huangdijia@gmail.com> * @license php version 3.0 {@link http://www.php.net/license/3_0.txt} */ 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'] <= 1){ unset($list[$name]); }else{ $list[$name]['count'] -= 1; $list[$name]['lasttime'] = time(); } self::__setlist($list); } } /** * getcount * * @static * @access public * @return void */ static public function getcount(){ $list = self::__getlist(); $name = self::__getname(); return $list[$name]['count']; } /** * getmaxnum * * @static * @access public * @return void */ static public function getmaxnum(){ $name = self::__getname(); return c($name) ? c($name) : self::process_maxnum; } /** * getname * * @static * @access public * @return void */ static public function getname(){ return self::__getname(); } /** * isovertop * * @static * @access public * @return void */ static public function isovertop(){ return (self::getcount() > 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; } } ?>
调用方法:
<?php class indexaction extends action { // 初始化模块 public function _initialize(){ parent::_initialize(); import('@.util.process'); process::start(); } function __destruct(){ process :: destory(); } public function index(){ c('index_index', 3); // 动态更改限制数, 默认为10 if(process::isovertop()) echo "超出限制"; else "未超出限制"; } } ?>
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。