If file_put_contents encounters an exclusive file lock, will the queue wait?

Source: Internet
Author: User
File_put_contents(&%039%test.txt & #039;, $ data, FILE_APPEND | LOCK_EX); the preceding statement takes a long time to write a large amount of data to a file, the subsequent file_put_contents will also write data to the file. because the file has an exclusive lock, the subsequent file_put... File_put_contents('test.txt ', $ data, FILE_APPEND | LOCK_EX );
The preceding statement takes a long time to write a large amount of data to a file. the subsequent file_put_contents will also write data to the file because the file has exclusive locks, will the subsequent file_put_contents become a queue waiting for the write operation of the previous file to be completed?

Reply content:

File_put_contents('test.txt ', $ data, FILE_APPEND | LOCK_EX );
The preceding statement takes a long time to write a large amount of data to a file. the subsequent file_put_contents will also write data to the file because the file has exclusive locks, will the subsequent file_put_contents become a queue waiting for the write operation of the previous file to be completed?

Yes. the implementation of file_put_contents is actually a simple implementation of the fopen, fwrite, fflush, and fclose operations. If LOCK_EX is added, a flock will be executed after fopen, and the code will be blocked until it is locked. This wait is sorted by queue.

In addition, writing too large files may exceed the maximum execution event of PHP, posing a risk of data loss. if data is important to you, we recommend that you implement a large data write queue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.