Home  >  Article  >  Backend Development  >  Will file_put_contents queue up and wait when it encounters a file exclusive lock?

Will file_put_contents queue up and wait when it encounters a file exclusive lock?

WBOY
WBOYOriginal
2016-10-17 09:30:162081browse

file_put_contents('test.txt',$data,FILE_APPEND|LOCK_EX);
Such as the above statement, if a large amount of data is being written to a file, it will take a long time, and subsequent file_put_contents must also be written to the file Input data, since the file has an exclusive lock, will subsequent file_put_contents become a queue to wait for the completion of the previous file write operation

Reply content:

file_put_contents('test.txt',$data,FILE_APPEND|LOCK_EX);
Such as the above statement, if a large amount of data is being written to a file, it will take a long time, and subsequent file_put_contents must also be written to the file Input data, since the file has an exclusive lock, will subsequent file_put_contents become a queue to wait for the completion of the previous file write operation

Yes, the implementation of file_put_contents is actually a simple implementation of the set of operations of fopen, fwrite, fflush, and fclose. If LOCK_EX is added, fopen will also execute a flock, and then this code will block until the file lock is released before continuing to execute. This waiting is sorted in a queue.

In addition, writing too large files may exceed the maximum execution event of PHP, and there is a risk of losing data. If the data is important to you, it is recommended to implement a large-volume write queue by yourself

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