-
-
define('DIR_PHP_EXEC', 'php');
- define('DIR_MAIN_LE__EC', __FILE__LE__ EXLE__EC');
- define('DIR_TMP', '/tmp');
- require_once('my_process.php');
class pp extends my_process_base {
- public function run($ param = null) {
- for ($i = 0; $i echo "111 $paramn";
- sleep(1);
- }
- }
- }
init_my_process();
- $obj = $GLOBALS['gal_obj_process_m'];
- if ($obj->is_main ()) {
- $
- $ obj->run_task('pp', 'a');
- $obj->run_task('pp', 'b');
- $obj->; run_task('pp', 'c');
- $obj->run_task('pp', 'd');
- //$obj->run_task('pp', 'b') ;
- $obj->set_max_run(10);
- $obj->run();
- }?>
複製程式碼 複製程式碼2,進程管理類
-
-
/**
- * @copyright 2007 movivi
- * @author 徐智*
- * $Id: getPage.php 11 2007-09-21 02:15:01Z fred $
- * 由bbs.it 修改- home.org
- */
- if (!define('DIR_PHP_EXEC')) Define( 'DIR_PHP_EXEC', 'php');
- //if (!define('DIR_MAIN_EXEC')) Define('DIR_MAIN_EXEC', '');
- if (!define('DIR_TMP')) Define (' DIR_TMP', '');
- /********/
- /* 初始化*/
- define('CMD_MAIN_PROCESS_KEY', 'main_process_key');
- define('CMD_CHI child_process_name');
- define('CMD_CHILD_PROCESS_PARAM', 'child_process_param');
function init_my_process() {
- $GLGLALS"[p>function init_my_process() {
- $GL_ALS"['d$key = $GLOBALS['gal_obj_cmd']->get_value(CMD_MAIN_PROCESS_KEY);
- $key = $key === false? '' : $key;
- $GLOBALS['gal_obj_proces_new_obj] =Fgal_obj] my_process_m($key);
- if (!$GLOBALS['gal_obj_process_m']->is_main()) $GLOBALS['gal_obj_process_m' ]->run() ;
- }
- /**
- * php多進程類別
- *
- * 你需要從這個物件繼承,然後實作你自己的run處理
- */
- 抽象類別my_process_base {
- public function __construct($auto_run=true , $name='') {
- }
public function __destruct() {
- echo "@endn";
- }
抽象公共函數run($param = null);
- }
- class my_cmd_argv {
- private $cmd_argv = array();
- public function __construct() {
- $argv = $_SERVER['argv'];
- for ($我=1; $i $ cmd = Explode('=', $argv[$i]);
- $this->cmd_argv[$cmd[0]] = isset($cmd[1]) ? $cmd[1] : '';
- }
- }
public function get_key($key) {
- return isset($this->cmd_argv[ $key] );
- }
public function get_value($key) {
- return isset($this->cmd_argv[$key]) ? : false;
- }
- }
/**
- * php多進程管理類別
- * 可以在PHP中實現多進程處理,只限在控制台方式使用
- * 目前的訊號實現機制採用檔案方式
- *
- */
- class my_process_m {
- /**
- * @var array $task_list
- * 進程清單
- */
- private $task_list = array();
- private $lock_list = array();
- private $lock = null;
- private $is_main = false;
- private $max_run = 3600000;
原生函數release_lock($key = null) {
- $lock = &$this->lock_list;
- if (!is_null ($key)) {
- $key = md5($ this->build_lock_id($key));
- if (isset($lock[$key])) {
- if (is_resource($ lock[$key][0])) fclose($lock[$ key][0]);
- unlink($lock[$key][1]);
- unset($lock[$key] );
- }
- 回傳true;
- }
foreach ($lock as $k => $h) {
- if (is_resource($h)) fclose($h);
- unset($lock[$k ) ]);
- }
- return true;
- }
創函數release_task($key = null) {
- $task = &$this->task_list ;
- if (!is_null($key) && isset($task[$key])) {
- if (is_resource($task[$key])) pclose($task[$key]);
- unset($task[ $key]);
- } else {
- foreach ($task as $k => $h) {
- if (is_resource($h)) pclose($h) ;
- unset( $task[$k]);
- }
- }
- 回傳true;
- }
-
- 初始化函數build_lock_id($key) {
-
- 初始化函數build_lock_id($key) {
- 回傳DIR_TMP 。 get_value (CMD_CHILD_PROCESS_NAME);
- $param = $GLOBALS['gal_obj_cmd']->get_value(CMD_CHILD_PROCESS_PARAM);
- $param = $param == '' ? null : unserialize(base64_decode(trim($param)));
- $obj = new $class();
- $obj->run($param);
- $this->task_list[ ] = $obj;
- }
public function __construct($lock='') {
- if ($lock === '') {
- $ this->is_main = true;
- $key = md5(uniqid()) 。 '_main.my_process';
- $lock = array($key, $this->get($key));
- } else {
- $this->is_main = false;
- $lock = array($lock, 0);
- }
- $this->lock = $lock;
- }
public function __destruct() {
- $this->release_lock();
- $this->release_task();
- }
/**
- * 停止所有程序
- *
- */
- 公用函數stop_all() {
- }
/**
- * 是否為主程序
- *
- */
- public function is_main() {
- return $this->is_main;
- }
/**
- * 是不是已經存在一個活動訊號
- *
- * @param string $key
- * @return bool
- */
- public function exit($key) {
- return file_exists($this->build_lock_id($key));
- } p>
/**
- * 取得一個訊號
- *
- * @param string $key
- * @param int $max_acquire 最大請求阻塞數量
- * @return mix 如果成功回傳一個訊號ID
- *
- */
- public function get($key, $max_acquire=5) {
- $fn = $this->build_lock_id($key);
- if (isset($this->lock_list[md5($fn)])) return false;
- $id = fopen($fn, 'a+');
- if ($id) $ this ->lock_list[md5($fn)] = array($id, $fn);
- return $id;
- }
/**
- * 釋放一個訊號
- *
- * @param string $key
- * @return bool 如果成功回傳一個訊號true
- *
- */
- public function remove($key) {
- return $this->release_lock($key);
- }
/**
- * 取得一個訊號
- *
- * @param string $id 訊號ID
- * @param bool $block 是否阻塞
- */
- public function acquire($id, $block=false) {
- if ($block) {
- return 群集($id, LOCK_EX);
- } else {
- return群集($id, LOCK_EX + + LOCK_NB);
- }
- }
/**
- * 釋放一個訊號
- *
- */
- 公用函數release($id) {
- farm($id, LOCK_UN) ;
- }
- public function run_task($process_name, $param=null) {
- $this->task_list[] = popen(DIR_PHP_EXEC . '-f'。DIR_MAIN_EXEC 。 ' - '
- > '。 CMD_CHILD_PROCESS_NAME 。 '=' 。 ] . '" ',
- 'r');
- }
公用函數run($auto_run = true) {
- if ($this->is_main) {
- $ps = &$this->task_list;
- $max_run = &$this->max_run;
- $id = 0;
- do {
- //echo "process-- - ----------: n";
- $c = 0;
- foreach ($ps as $k => $h) {
- $c++;
- $msg = fread($h, 8000);
- if (substr($msg, -5, 4) === '@end') {
- echo " 結束進程:[$k][$id] echo n {$msg} n";
- $this->release_task($k);
- } else {
- echo "process:[$k][$id] echo n{$msg} n" ;
- }
- }
- sleep(1);
- } while ($auto_run && $id++ 0);
- } else {
- $this- > run_child_process();
- }
- }
公用函數set_max_run ($max=1000) {
- $this->max_run = $max;
- }
- $this->max_run = $max;
- }
- }
- ?>
複製程式碼
|