PHP process synchronization code instance, php process instance _ PHP Tutorial

Source: Internet
Author: User
Tags flock
PHP process synchronization code instance and php process instance. PHP process code synchronization instance. php process instances often encounter such a situation. scheduled tasks are scheduled to run a php program in the background, and sometimes need to be executed manually, multiple users may need PHP process synchronization code instances and php process instances.

This is often the case where a scheduled task is scheduled to run a php program in the background and sometimes needs to be executed manually. many people may need to execute this program. if the task lasts for a long time, it is easy to cause repeated execution, so 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 ");
}

Pipeline often encounters such a situation where a scheduled task is scheduled to run a php program in the background, and sometimes it needs to be executed manually. it may be required by many people...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.