当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP进程同步代码实例

PHP进程同步代码实例

2018年07月03日  | 移动技术网IT编程  | 我要评论

经常遇到这样一种情况,计划任务定时后台执行某个php程序,有时候也需要手动执行,可能多个人都需要执行这个程序,如果任务持续时间非常长,就很容易造成重复执行,所以就开发了下面的类。

作用:在实际代码运行前检查与当前相同操作的进程是否正在运行,高并发运行是可靠的,运行中的进程中途异常中断不会产生任何影响。

构造方法传递pid文件目录的绝对路径,需要自己保证不同进程对应不同pid文件。

复制代码 代码如下:

<?php

/*
 * 同一个php进程只运行一次,根据进程名字判断是否为排重进程,只能运行于linux,高并发条件下是并发安全的。
 */

class syncprocess {

 private $pidfile;

 function __construct($pidfile) {
  $this->pidfile = $pidfile;
 }

 /**
  * 非阻塞方式返回进程是否正在运行
  */
 function check() {
  if (php_os == 'linux') {
   $pidfile = $this->pidfile;
   if (!empty($pidfile)) {
    $flag = false;
    $piddir = dirname($pidfile);
    if (is_dir($piddir)) {
     $flag = true;
    }
    if ($flag) {
     $running = true;
     clearstatcache(true, $this->pidfile);
     if (!file_exists($this->pidfile))
      file_put_contents($this->pidfile, '', lock_ex);
     $f = fopen($this->pidfile, 'r+');
     if (flock($f, lock_ex ^ lock_nb)) {
      $pid = trim(fgets($f));
      if (!$this->is_process_running($pid)) {
       $running = false;
      }
     }
     if (!$running) {
      fseek($f, 0);
      ftruncate($f, 0);
      fwrite($f, getmypid());
     }
     flock($f, lock_un);
     fclose($f);
     return $running;
    } else {
     debug_print("pid file($pidfile) is invalid", e_user_warning);
    }
   } else {
    debug_print("pid file cant't be empty", e_user_warning);
   }
  } else {
   debug_print(__class__ . ' can only run in linux', e_user_warning);
   return true;
  }
 }

 /**
  * 如果正在运行或者发生未知错误返回true,如果没有运行返回false
  * @param mixed $pid
  */
 private function is_process_running($pid) {
  if (is_numeric($pid) && $pid > 0) {
   $output = array();
   $line = exec("ps -o pid --no-headers -p $pid", $output);
   //返回值有空格
   $line = trim($line);
   if ($line == $pid) {
    return true;
   } else {
    if (empty($output)) {
     return false;
    } else {
     if (php_sapi_name() == 'cli')
      $n = "\n";
     else
      $n = "<br>";
     //到这一步的话应该是出什么问题了
     $output = implode($n, $output);
     debug_print($output, e_user_warning);
     return true;
    }
   }
  }else {
   return false;
  }
 }

}

demo:

复制代码 代码如下:

$sync = new syncprocess(app_path . '/data/pid'.implode('', $this->getroute()));
if ($sync->check()) {
 exit("process is running\n");
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网