PHP process Synchronization Code instance, PHP process Instance _php tutorial

Source: Internet
Author: User
Tags flock

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 ...

  • 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.