PHP process Synchronization Code instance, PHP process instance
Often encountered in such a situation, scheduled tasks in the background to execute a PHP program, sometimes need to be manually executed, many people may need to execute this program, if the task duration is very long, it is easy to cause repeated execution, so developed the following class.
Role: Before the actual code runs, check that the process is running with the same operations as the current operation, that high concurrency runs are reliable, and that abnormal interrupts in the middle of a running process have no effect.
The construction method passes the absolute path of the PID file directory, and needs to ensure that different processes correspond to different PID files.
Copy the Code code as follows:
<?php
/*
* The same PHP process runs only once, according to the process name to determine whether it is a process, only run on Linux, high concurrency is concurrency-safe.
*/
Class Syncprocess {
Private $pidFile;
function __construct ($pidFile) {
$this->pidfile = $pidFile;
}
/**
* Non-blocking mode returns whether the process is running
*/
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;
}
}
/**
* Returns true if running or unknown error occurs, false if not run
* @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);
The return value has a space
$line = Trim ($line);
if ($line = = $pid) {
return true;
} else {
if (empty ($output)) {
return false;
} else {
if (php_sapi_name () = = ' CLI ')
$n = "\ n";
Else
$n = "
";
It's going to be a problem at this stage.
$output = Implode ($n, $output);
Debug_print ($output, e_user_warning);
return true;
}
}
}else {
return false;
}
}
}
Demo:
Copy the Code code as follows:
$sync = new Syncprocess (App_path. '/data/pid '. Implode (", $this->getroute ()));
if ($sync->check ()) {
Exit ("process is running\n");
}
http://www.bkjia.com/PHPjc/957119.html www.bkjia.com true http://www.bkjia.com/PHPjc/957119.html techarticle PHP Process Synchronization code instances, PHP process instances often encounter such a situation, scheduled tasks scheduled to execute a PHP program in the background, and sometimes need to manually execute, it may be more people need ...