The thinkphp process count class Process Usage example detailed _php instance

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.