Describes how to implement process locks and multi-process in php

Source: Internet
Author: User
This article describes how to implement a single process lock, how to implement a multi-process lock, how to implement a process lock in yii2, and how to kill the process, if you need it, you can refer to it for reference. let's take a look at it. This article describes how to implement a single process lock, how to implement a multi-process lock, how to implement a process lock in yii2, and how to kill the process, if you need it, you can refer to it for reference. let's take a look at it.

Why do we need a process lock?

The main function is to prevent you from executing the same program repeatedly. it is mainly used in crontab. when you set a scheduled task and execute it once every minute, if no process lock is applied, if the previous process is not completed. A new process is generated every minute. After a process lock is added, the system checks whether the previous process lock exists each time the scheduled task is executed. if the previous process lock exists, the system does not execute the lock.

1. single-process lock implementation

Let's just give an example. write a php script and name it process. php first. the code is as follows:

 

Then in the linux environment, execute this code and it will run normally for the first time, and then the prompt will be displayed during the second run:Process already exists.This prompt appears no matter how many times it is executed, indicating that the process has failed.

Php process. php & // & symbol indicates that php is run in the background

The name of the process lock in the preceding code can be changed slightly. the name of the process lock is determined based on the passed parameters. if the passed parameters are consistent, the same lock is applied.

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){}

In linux. Go to the directory where the file is located and execute php process. php 1, then open a new window and execute php process. php 1 will prompt the error Process already exists, but if you are executing php process. php 2 will run normally. Similarly, you can pass two parameters, such as php process. php 1 2. in this case, it corresponds to a new process lock. This implementation mainly uses the php parameters argc and argv. argc indicates the total number of parameters, and argv indicates which parameter is used for calling.

3. implement process lock in yii2

To implement a multi-process lock in yii2, you must inherit the controller in the console of yii, so as to transmit parameters,use yii\console\Controller;
Then in crontab, run the following command: run the scheduled script with the yii command. Then the process lock code is the same as above.

* ***** Root/usr/local/bin/php/data/www/html/newVanish/yii controller name/method parameters>/dev/null 2> & 1

4. command for killing a process

This is definitely something to press the bottom of the box. I will not tell him the average person.
You can use a command to delete the corresponding process. The following command deletes all processes whose names are process_name. You only need to change process_name to the name of the process you want to delete.

ps aux|grep process_name |grep -v grep|awk '{print $2}'|xargs kill -9

Summary

The above is a detailed description of the methods for implementing process locks and multi-process in php. For more information, see other related articles in the first PHP community!

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.