The example in this article describes the process usage of the thinkphp processes count class. Share to everyone for your reference. Specific as follows:
There is a requirement in the project: to limit the number of processes because a background task compares the bandwidth. Took some time to write the class, the current version of the function is relatively simple.
The Process.class.php file is as follows:
<?php/** * Process * * @package * @version $id $ * @copyright 2005-2011 sucop.com * @author Dijia Huang
* @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 '] >) {$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 () {$n ame = Self::__getname (); Return C ($name)? C ($name): self::P rocess_maxnum; }/** * GetName * * @static * @access public * @return void */static public Function GetName () {return Self::__getname (); }/** * Isovertop * * @static * @access public * @return void */static public Function Isovertop () {RE Turn (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::P rocess_key, NULL); }/** * __setlist * * @param mixed $list * @static * @access private * @return void */static private F Unction __setlist ($list =null) {if (!is_array ($list) | | empty ($list)) F (self::P rocess_key, NULL); Else F (self::P rocess_key, $list); }/** * __getlist * * @static * @access private * @return void */static Private function __getlist () { $list = F (self::P rocess_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; }}?>
Call Method:
<?phpclass Indexaction extends Action { //Initialize module public function _initialize () { parent::_initialize (); import (' @. Util.process '); Process::start (); } function __destruct () { Process:: Destory (); } Public Function Index () { C (' Index_index ', 3);//dynamic change limit number, default to "Process::isovertop ()" echo "Out of limit"; Else "not exceeding the limit"; }}? >
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.