+ -
当前位置:首页 → 问答吧 → 进程计数类Process.class.php

进程计数类Process.class.php

时间:2011-07-13

来源:互联网



项目中有一个需求:由于某一后台任务比较占带宽,所以要限制进程数。花了点时间,写了类,目前版本功能比较简单,源码:
  1. <?php
  2. class Process
  3. {
  4.     const PROCESS_KEY = '~Process';
  5.     const PROCESS_MAXNUM = 10;

  6.     static public function start(){
  7.         $list = self::__getList();
  8.         $name = self::__getName();
  9.         if(!isset($list[$name])){
  10.             $list[$name] = array('count'=>1, 'lasttime'=>time());
  11.         }else{
  12.             $list[$name]['count'] += 1;
  13.         }
  14.         self::__setList($list);
  15.     }

  16.     static public function destory(){
  17.         $list = self::__getList();
  18.         $name = self::__getName();
  19.         if(isset($list[$name])){
  20.             if($list[$name]['count'] <= 1) unset($list[$name]);
  21.             else $list[$name]['count'] -= 1;
  22.             self::__setList($list);
  23.         }
  24.     }

  25.     static public function getCount(){
  26.         $list = self::__getList();
  27.         $name = self::__getName();
  28.         return $list[$name]['count'];
  29.     }

  30.     static public function getMaxnum(){
  31.         $name = self::__getName();
  32.         return C($name) ? C($name) : self::PROCESS_MAXNUM;
  33.     }

  34.     static public function getName(){
  35.         return self::__getName();
  36.     }

  37.     static public function isOvertop(){
  38.         return (self::getCount() > self::getMaxnum());
  39.     }

  40.     static public function getLasttime(){
  41.         $list = self::__getList();
  42.         $name = self::__getName();
  43.         return $list[$name]['lasttime'];
  44.     }

  45.     static public function clear(){
  46.         F(self::PROCESS_KEY, null);
  47.     }

  48.     static private function __setList($list=null){
  49.         if(!is_array($list) || empty($list))
  50.             F(self::PROCESS_KEY, null);
  51.         else
  52.             F(self::PROCESS_KEY, $list);
  53.     }

  54.     static private function __getList(){
  55.         $list = F(self::PROCESS_KEY);
  56.         if(!is_array($list)) return array();
  57.         else return $list;
  58.     }

  59.     static private function __getName(){
  60.         return (defined('GROUP_NAME') ? GROUP_NAME.'_' : '') . MODULE_NAME . '_' . ACTION_NAME;
  61.     }
  62. }
  63. ?>
复制代码
调用方法:
  1. <?php
  2. class IndexAction extends Action
  3. {
  4.     // 初始化模块
  5.     public function _initialize(){
  6.         parent::_initialize();
  7.         import('@.Util.Process');
  8.         Process::start();
  9.     }
  10.    
  11.     function __destruct(){
  12.         Process :: destory();
  13.     }
  14.    
  15.     public function index(){
  16.         C('Index_index', 3); // 动态更改限制数, 默认为10
  17.         if(Process::isOvertop()) echo "超出限制";
  18.         else "未超出限制";
  19.     }
  20. }
  21. ?>
复制代码

作者: deeka   发布时间: 2011-07-13

修正了一些问题。
  1. <?php
  2. /**
  3. * Process
  4. *
  5. * @package
  6. * @version $id$
  7. * @copyright 2005-2011 SUCOP.COM
  8. * @author Dijia Huang <[email protected]>
  9. * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
  10. */
  11. class Process
  12. {
  13.     const PROCESS_KEY = '~Process';
  14.     const PROCESS_MAXNUM = 10;

  15.     /**
  16.      * start
  17.      *
  18.      * @static
  19.      * @access public
  20.      * @return void
  21.      */
  22.     static public function start(){
  23.         $list = self::__getList();
  24.         $name = self::__getName();
  25.         if(!isset($list[$name])){
  26.             $list[$name] = array('count'=>1, 'lasttime'=>time());
  27.         }else{
  28.             if((time()-$list[$name]['time']) > 600){
  29.                 $list[$name]['count'] = 1;
  30.             }else{
  31.                 $list[$name]['count'] += 1;
  32.             }
  33.         }
  34.         self::__setList($list);
  35.     }

  36.     /**
  37.      * destory
  38.      *
  39.      * @static
  40.      * @access public
  41.      * @return void
  42.      */
  43.     static public function destory(){
  44.         $list = self::__getList();
  45.         $name = self::__getName();
  46.         if(isset($list[$name])){
  47.             if($list[$name]['count'] <= 1){
  48.                 unset($list[$name]);
  49.             }else{
  50.                 $list[$name]['count'] -= 1;
  51.                 $list[$name]['lasttime'] = time();
  52.             }
  53.             self::__setList($list);
  54.         }
  55.     }

  56.     /**
  57.      * getCount
  58.      *
  59.      * @static
  60.      * @access public
  61.      * @return void
  62.      */
  63.     static public function getCount(){
  64.         $list = self::__getList();
  65.         $name = self::__getName();
  66.         return $list[$name]['count'];
  67.     }

  68.     /**
  69.      * getMaxnum
  70.      *
  71.      * @static
  72.      * @access public
  73.      * @return void
  74.      */
  75.     static public function getMaxnum(){
  76.         $name = self::__getName();
  77.         return C($name) ? C($name) : self::PROCESS_MAXNUM;
  78.     }

  79.     /**
  80.      * getName
  81.      *
  82.      * @static
  83.      * @access public
  84.      * @return void
  85.      */
  86.     static public function getName(){
  87.         return self::__getName();
  88.     }

  89.     /**
  90.      * isOvertop
  91.      *
  92.      * @static
  93.      * @access public
  94.      * @return void
  95.      */
  96.     static public function isOvertop(){
  97.         return (self::getCount() > self::getMaxnum());
  98.     }

  99.     /**
  100.      * getLasttime
  101.      *
  102.      * @static
  103.      * @access public
  104.      * @return void
  105.      */
  106.     static public function getLasttime(){
  107.         $list = self::__getList();
  108.         $name = self::__getName();
  109.         return $list[$name]['lasttime'];
  110.     }

  111.     /**
  112.      * clear
  113.      *
  114.      * @static
  115.      * @access public
  116.      * @return void
  117.      */
  118.     static public function clear(){
  119.         F(self::PROCESS_KEY, null);
  120.     }

  121.     /**
  122.      * __setList
  123.      *
  124.      * @param mixed $list
  125.      * @static
  126.      * @access private
  127.      * @return void
  128.      */
  129.     static private function __setList($list=null){
  130.         if(!is_array($list) || empty($list))
  131.             F(self::PROCESS_KEY, null);
  132.         else
  133.             F(self::PROCESS_KEY, $list);
  134.     }

  135.     /**
  136.      * __getList
  137.      *
  138.      * @static
  139.      * @access private
  140.      * @return void
  141.      */
  142.     static private function __getList(){
  143.         $list = F(self::PROCESS_KEY);
  144.         if(!is_array($list)) return array();
  145.         else return $list;
  146.     }

  147.     /**
  148.      * __getName
  149.      *
  150.      * @static
  151.      * @access private
  152.      * @return void
  153.      */
  154.     static private function __getName(){
  155.         return (defined('GROUP_NAME') ? GROUP_NAME.'_' : '') . MODULE_NAME . '_' . ACTION_NAME;
  156.     }
  157. }
  158. ?>
复制代码

作者: deeka   发布时间: 2011-07-14

支持分享,有空研究下。

作者: cl360   发布时间: 2011-07-14