PHP file lock solves high concurrency

不言
Release: 2023-03-22 22:44:01
Original
3619 people have browsed it

This article will share with you about the method of php file lock to solve high concurrency. Friends in need can refer to the content of this article

php file lock to solve high concurrency

Create a new .txt file without writing anything in the file.

[1]. Blocking (waiting) mode: (As long as other processes have locked the file, the current process will wait for other processes to unlock the file)

<?php//连接数据库$con=mysqli_connect("192.168.2.186","root","root","test");//查询商品数量是否大于0,大于0才能下单,并减少库存$fp = fopen("lock.txt", "r");//加锁if(flock($fp,LOCK_EX))
{    $res=mysqli_fetch_assoc(mysqli_query($con,&#39;SELECT total FROM shop WHERE id=1 LIMIT 1&#39;));    if($res[&#39;total&#39;]>0){mysqli_query($con,&#39;UPDATE shop SET total=total-1  WHERE id=1&#39;);}    //执行完成解锁
    flock($fp,LOCK_UN);
}//关闭文件fclose($fp);unset($res);
mysqli_close($con);?>
Copy after login
Copy after login

[2]. Non-blocking ( Waiting) mode: (As long as other processes have locked the file, the current process will not wait for other processes to unlock the file and return directly)

<?php//连接数据库$con=mysqli_connect("192.168.2.186","root","root","test");//查询商品数量是否大于0,大于0才能下单,并减少库存$fp = fopen("lock.txt", "r");//加锁if(flock($fp,LOCK_EX | LOCK_NB))
{    $res=mysqli_fetch_assoc(mysqli_query($con,&#39;SELECT total FROM shop WHERE id=1 LIMIT 1&#39;));    if($res[&#39;total&#39;]>0){mysqli_query($con,&#39;UPDATE shop SET total=total-1  WHERE id=1&#39;);}    //执行完成解锁
    flock($fp,LOCK_UN);
}//关闭文件fclose($fp);unset($res);
mysqli_close($con);?>
Copy after login
Copy after login

If connecting to the database takes time, here is a simple demo , which can be understood more intuitively.

demo.php

<?php$fp = fopen("file_lock.txt", "r");// 加锁if(flock($fp, LOCK_EX))
{
    sleep(10);    echo 1;    //执行完成解锁
    flock($fp,LOCK_UN);
} else {    echo 2;
}//关闭文件fclose($fp);
Copy after login
Copy after login

demo2.php

<?php$fp = fopen("file_lock.txt", "r");// 加锁(如果改成flock($fp, LOCK_EX | LOCK_NB),demo2.php会直接返回2,否则会等待demo.php执行完返回1)if(flock($fp, LOCK_EX))
{    echo 1;
} else {    echo 2;
}//关闭文件fclose($fp);
Copy after login
Copy after login

Run two files at the same time, and then modify the locking mechanism in demo2, you can see blocking (waiting ) mode and non-blocking (waiting) mode
difference.

But this will cause the queue to be blocked. If 10 people write to the database at the same second, it will be blocked. The 10th person will wait for the first 9 to be executed before executing!

php file lock solves high concurrency

Create a new .txt file without writing anything in the file.

[1]. Blocking (waiting) mode: (As long as other processes have locked the file, the current process will wait for other processes to unlock the file)

<?php//连接数据库$con=mysqli_connect("192.168.2.186","root","root","test");//查询商品数量是否大于0,大于0才能下单,并减少库存$fp = fopen("lock.txt", "r");//加锁if(flock($fp,LOCK_EX))
{    $res=mysqli_fetch_assoc(mysqli_query($con,&#39;SELECT total FROM shop WHERE id=1 LIMIT 1&#39;));    if($res[&#39;total&#39;]>0){mysqli_query($con,&#39;UPDATE shop SET total=total-1  WHERE id=1&#39;);}    //执行完成解锁
    flock($fp,LOCK_UN);
}//关闭文件fclose($fp);unset($res);
mysqli_close($con);?>
Copy after login
Copy after login

[2]. Non-blocking ( Waiting) mode: (As long as other processes have locked the file, the current process will not wait for other processes to unlock the file and return directly)

<?php//连接数据库$con=mysqli_connect("192.168.2.186","root","root","test");//查询商品数量是否大于0,大于0才能下单,并减少库存$fp = fopen("lock.txt", "r");//加锁if(flock($fp,LOCK_EX | LOCK_NB))
{    $res=mysqli_fetch_assoc(mysqli_query($con,&#39;SELECT total FROM shop WHERE id=1 LIMIT 1&#39;));    if($res[&#39;total&#39;]>0){mysqli_query($con,&#39;UPDATE shop SET total=total-1  WHERE id=1&#39;);}    //执行完成解锁
    flock($fp,LOCK_UN);
}//关闭文件fclose($fp);unset($res);
mysqli_close($con);?>
Copy after login
Copy after login

If connecting to the database takes time, here is a simple demo , which can be understood more intuitively.

demo.php

<?php$fp = fopen("file_lock.txt", "r");// 加锁if(flock($fp, LOCK_EX))
{
    sleep(10);    echo 1;    //执行完成解锁
    flock($fp,LOCK_UN);
} else {    echo 2;
}//关闭文件fclose($fp);
Copy after login
Copy after login

demo2.php

<?php$fp = fopen("file_lock.txt", "r");// 加锁(如果改成flock($fp, LOCK_EX | LOCK_NB),demo2.php会直接返回2,否则会等待demo.php执行完返回1)if(flock($fp, LOCK_EX))
{    echo 1;
} else {    echo 2;
}//关闭文件fclose($fp);
Copy after login
Copy after login

Run two files at the same time, and then modify the locking mechanism in demo2, you can see blocking (waiting ) mode and non-blocking (waiting) mode
difference.

But this will cause the queue to be blocked. If 10 people write to the database at the same second, it will be blocked. The 10th person will wait for the first 9 to be executed before executing!

Related recommendations:

Let’s talk about PHP file locks, shared locks and exclusive locks. Notes

Detailed explanation of PHP file lock concurrent operations



The above is the detailed content of PHP file lock solves high concurrency. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!