search
HomeWeChat AppletMini Program DevelopmentHow PHP uses file locks to solve high concurrency problems

This article mainly introduces the use of file locks in PHP to solve high concurrency problems. It analyzes the related usage skills of PHP blocking mode and non-blocking mode file locks to solve high concurrency problems in the form of examples. Friends who need it can refer to it. I hope it can help. to everyone.

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);
?>

【二】.Non-blocking (waiting) mode:(As long as there are other The process has 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);
?>

If connecting to the database takes time, here is a simple tip Demo 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);

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);

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

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!


The above is the detailed content of How PHP uses file locks to solve high concurrency problems. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.