PHP file function flock
? ? ? ? In the case of concurrency, how can php write files? In fact, this problem is not just the problem that PHP faces. Both threads and processes encounter shared resource write conflicts when concurrent writes. Software development process, write conflicts everywhere, such as multi-threaded write shared variables, such as database multi-connection concurrent write data, such as multi-process write files and so on. So how do you deal with all this? The prevailing approach is to give exclusive locks (write locks) to shared resources.
? ? ? ? PHP in the third version of the time provides a function flock, as the name implies, the file lock operation function. File lock mechanism is dependent on the host file system, that is, how a lock method, is the host file system, flock is just a shell function, which calls the file system lock mechanism.
? ? ? ? There are two types of lock, read lock and write lock. Write lock is also called exclusive lock, is a thread or process exclusive resources, other threads or processes can not use resources, to ensure that the write data is not disturbed, does not conflict; Read locks are also called shared locks, allowing multiple threads or processes to read at the same time, but not write operations.
Function Prototypes:
?
Flock ($handle, $operation, & $wouldblock = null);
?
Parameter description:
$handle//File pointer
$operation//Lock type
$operation have several available values: LOCK_EX "Write lock", lock_sh "read lock", Lock_un "release lock"
? ? ? ? Let's see an example.
?
function FileWrite ($file) {$fp = fopen ($file, ' a '), Flock ($FP, LOCK_EX),//write lock/* Write Data */fwrite ($fp, "1"), Fwrite ($FP, "2"); Fwrite ($fp, "3\r\n"); Flock ($fp, lock_un);//Release lock fclose ($FP);} FileWrite (' D:/txt.txt ');
? ? ? ? Using JMeter for concurrent testing, 3,000 requests will be found in the file with 3000 lines of 123 (regardless of Apache or nginx performance bottleneck).
?
? ? ? ? When doing concurrent testing with JMeter, open this file with Notepad, pity Dorado something, and then click Save, and you will find the situation
? ? ? ? This is because the file is locked and the Notepad process cannot write.
? ? ? ? Front said, flock function lock mechanism is actually the file system lock mechanism, it encapsulates some types of file system lock mechanism, some file system flock not support, according to PHP manual, FAT, ntf and other such old-fashioned file system and network file system, flock does not support. I have not measured it.
? ? ? ? The PHP manual also says that flock is a process-level, multi-threaded time that does not work. Perhaps we will have doubts, PHP is not the concept of threading. Let's put PHP on a server that supports multi-threading, so let's think about it. Measurement.
Analysis to this step, you will find that flock depends on the host, environmental requirements, which deeply hurt the portability of the code. Afraid!! Do not fear, in fact, the injury is not much deep, flock still can meet most of the environment.
?
? ? ? ? For the transplant of flock slightly inadequate, we began to study alternative methods, I also pay attention to the next, found that the online spread of the following code
?
function FileWrite ($file, $content) {$lock = $file. '. Lock '; while (true) {if (file_exists ($lock)) {usleep (100);} Else{touch ($lock);//Lock File_put_contents ($file, $content, file_append);//write File @unlink ($lock);//delete lock break;}}} FileWrite (' d:/txt.txt ', ' i m a Phper ');
? ? ? ? Do you think this piece of code has a lock function?? Test and test!! In fact, do not test, a look on the lock,file_exists can be executed in parallel, when the two processes at the same time to file_exists, judge the so-called lock file does not exist, the result is not a conflict.
Lock, is actually the concurrent access queued blocking serialization, and then processing access, the upper part of the code is not even queued, how can lock it.
? ? At this time, you may ask multiple processes to execute flock simultaneously, not parallel? Yes, the execution of flock is parallel, it just means that the application lock is parallel, flock internal tune is the host file system, the lock request will naturally queue processing.
?