This article mainly introduces the PHP process synchronization code example. This article provides the implementation code directly. If you need it, you can refer to this situation and often run a php program in the background when you plan tasks, sometimes you also need to execute the program manually. Many people may need to execute this program. If the task lasts for a long time, it will easily lead to repeated execution. Therefore, the following class is developed.
Purpose: Check whether the process with the same operation is running before the actual code is run. High concurrency is reliable, and abnormal interruptions during running process will not affect the process.
The constructor transmits the absolute path of the pid file directory. You must ensure that different processes correspond to different pid files.
The Code is as follows:
<? Php
/*
* The same PHP process runs only once, and determines whether it is a deduplication process based on the process name. It can only run in linux and is highly concurrent and secure.
*/
Class SyncProcess {
Private $ pidFile;
Function _ construct ($ pidFile ){
$ This-> pidFile = $ pidFile;
}
/**
* Return whether the process is running in non-blocking mode.
*/
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 is returned if the instance is running or an unknown error occurs. false is returned if the instance is not running.
* @ 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 contains spaces.
$ Line = trim ($ line );
If ($ line = $ pid ){
Return true;
} Else {
If (empty ($ output )){
Return false;
} Else {
If (php_sapi_name () = 'cli ')
$ N = "\ n ";
Else
$ N ="
";
// What should be the problem at this step?
$ Output = implode ($ n, $ output );
Debug_print ($ output, E_USER_WARNING );
Return true;
}
}
} Else {
Return false;
}
}
}
Demo:
The Code is as follows:
$ Sync = new SyncProcess (APP_PATH. '/data/pid'. implode ('', $ this-> getRoute ()));
If ($ sync-> check ()){
Exit ("process is running \ n ");
}