PHP Example of Using File Locks to Solve High Concurrency Problems

  • 2021-09-16 06:28:42
  • OfStack

This article illustrates how PHP uses file locks to solve the problem of high concurrency. Share it for your reference, as follows:

Create a new. txt file, and you don't have to write anything in the file.

"1". Blocking (waiting) mode: (The current process will wait until another process unlocks the file as long as another process has already locked the file)


<?php
// Connect to a database 
$con=mysqli_connect("192.168.2.186","root","root","test");
// Query whether the item quantity is greater than 0, Greater than 0 To place an order , And reduce inventory 
$fp = fopen("lock.txt", "r");
// Lock 
if(flock($fp,LOCK_EX))
{
  $res=mysqli_fetch_assoc(mysqli_query($con,'SELECT total FROM shop WHERE id=1 LIMIT 1'));
  if($res['total']>0){mysqli_query($con,'UPDATE shop SET total=total-1 WHERE id=1');}
  // Execute complete unlock 
  flock($fp,LOCK_UN);
}
// Close a file 
fclose($fp);
unset($res);
mysqli_close($con);
?>

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


<?php
// Connect to a database 
$con=mysqli_connect("192.168.2.186","root","root","test");
// Query whether the item quantity is greater than 0, Greater than 0 To place an order , And reduce inventory 
$fp = fopen("lock.txt", "r");
// Lock 
if(flock($fp,LOCK_EX | LOCK_NB))
{
  $res=mysqli_fetch_assoc(mysqli_query($con,'SELECT total FROM shop WHERE id=1 LIMIT 1'));
  if($res['total']>0){mysqli_query($con,'UPDATE shop SET total=total-1 WHERE id=1');}
  // Execute complete unlock 
  flock($fp,LOCK_UN);
}
// Close a file 
fclose($fp);
unset($res);
mysqli_close($con);
?>

If it takes time to connect to the database, there is a simple small demo below, which can be understood more intuitively.

demo.php


<?php
$fp = fopen("file_lock.txt", "r");
//  Lock 
if(flock($fp, LOCK_EX))
{
  sleep(10);
  echo 1;
  // Execute complete unlock 
  flock($fp,LOCK_UN);
} else {
  echo 2;
}
// Close a file 
fclose($fp);

demo2.php


<?php
$fp = fopen("file_lock.txt", "r");
//  Lock (if changed to flock($fp, LOCK_EX | LOCK_NB) , demo2.php Will return directly 2 Or wait demo.php Return after execution 1 ) 
if(flock($fp, LOCK_EX))
{
  echo 1;
} else {
  echo 2;
}
// Close a file 
fclose($fp);

You can see the difference between blocking (wait) mode and non-blocking (wait) mode by running both files at the same time and modifying the locking mechanism in demo2.

But this will lead to queue blockage, if 10 people write to the database with 1 second, it will be blocked, and the 10th person will wait for the first 9 to be executed before it will be executed!

More readers interested in PHP can check the topics of this site: "php File Operation Summary", "PHP Common Traversal Algorithms and Skills Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithms Summary", "PHP Array (Array) Operation Skills Complete Book", "php String (string) Usage Summary" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: