Introduction to the method of implementing process lock and multi-process in PHP

Source: Internet
Author: User
Tags flock yii
This article introduces the process of single process lock implementation, process lock implementation of multi-process, the implementation of process lock in YII2 and the command to kill the process, the need for friends can refer to, the following to see together.

Why do I need a process lock?

The main function is to prevent you to repeat the same program, mainly used in Crontab, when you set a scheduled task, and then every minute to execute, if not the process lock, the previous process is not completed. A new process is generated every minute. After adding the process lock, each time the scheduled task executes, it will determine whether the previous process lock exists, if there is no execution.

1. Process lock implementation for single-process scenarios

Just come to an example, write a PHP script, first named process.php Bar, the code is as follows:

<?php$lock_file = dirname (file).  "/process.lock", $lock _file_handle = fopen ($lock _file, ' W '), if ($lock _file_handle = = = False) die ("Can not create lock file {$lock _file}\n "), if (!flock ($lock _file_handle, LOCK_EX + lock_nb)) {Die (date (" Y-m-d h:i:s "). "Process already exists.\n");} while (1) {}

Then in the Linux environment, execute this code, the first time it will run normally, and then the second run will be prompted: The next time Process already exists. no matter how many times will appear this prompt indicates that the process failed.

PHP process.php &//& symbol to run PHP in the background


The above code can also be used in the script of your timed plan, plus at the beginning. is equivalent to adding a process lock to a single script.

2. Process lock for multi-process

The above is a lock corresponding to a PHP script, that to use process lock to achieve multi-process, in fact, let a process can have more than one process lock is good.
The first part of the process lock of the above code can be implemented with a slight change, according to the parameters passed in to determine the name of the process lock, if the parameters passed in the same lock corresponding to the same.

if (! ( $ARGC > 1)) {$lock _file = dirname (file). "/process.lock";} else{unset ($argv [0]); $lock _file = dirname (file). "/process". Implode ('. ', $argv). ". Lock ";} $lock _file_handle = fopen ($lock _file, ' W '), if ($lock _file_handle = = = False) {die ("Can not create lock file $lock _file\n"); }if (!flock ($lock _file_handle, LOCK_EX + lock_nb)) {Die (date ("Y-m-d h:i:s"). "Process already exists.\n");} while (1) {}

Under the Linux environment. Enter the file directory, execute PHP process.php 1, and then open a new window, in the execution of PHP process.php 1, will prompt the process already exists this error, but if you are doing PHP process.php 2, it will run normally, similarly, you can pass two parameters, such as PHP process.php 1 2, so that the corresponding is a new process lock. This implementation is mainly used by the ARGC and argv the two PHP parameters, ARGC represents the total number of parameters, argv indicates the time of the specific invocation of which parameter.

3. Implementation of process lock in YII2

In Yii2 to implement a multi-process process lock, to inherit the controller in the console of Yii, so that the parameters can be passed, use yii\console\Controller;
and then in the crontab, the command is as follows, with Yii's own command to execute the timed script. Then the code for the process lock will be the same as above.

* * * * * * ROOT/USR/LOCAL/BIN/PHP/DATA/WWW/HTML/NEWVANISH/YII Controller name/method passed parameters >/dev/null 2>&1

4. command to kill a process

This is definitely the bottom of the thing, the General people I do not tell him.
The corresponding process can be deleted by a single command. The following command can be used to directly delete all processes whose name is Process_name. What to delete just change the process_name directly to the process name you want to delete.

PS aux|grep process_name |grep-v Grep|awk ' {print $} ' |xargs kill-9

Summarize

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.