file_put_contents遇见文件排他锁是是否会队列等待

WBOY
Release: 2016-10-17 09:30:16
Original
2074 people have browsed it

file_put_contents('test.txt',$data,FILE_APPEND|LOCK_EX);
如上语句,假如正在进行大数据量的文件写入操作,会占用较长时间,后续的file_put_contents也要向该文件写入数据,由于文件具有排他锁,后续的file_put_contents是否会成为队列进行等待上一个文件写操作执行完毕

回复内容:

file_put_contents('test.txt',$data,FILE_APPEND|LOCK_EX);
如上语句,假如正在进行大数据量的文件写入操作,会占用较长时间,后续的file_put_contents也要向该文件写入数据,由于文件具有排他锁,后续的file_put_contents是否会成为队列进行等待上一个文件写操作执行完毕

是,file_put_contents的实现其实就是fopen,fwrite,fflush,fclose的这一套操作的简便实现。如果加上LOCK_EX,fopen之后还会还会执行一个flock,之后这段代码会一直阻塞到获放文件锁后才会继续执行。这个等待是以队列方式进行排序的.

另外过大的文件写入,可能会超过PHP的最大执行事件,有丢失数据的风险,如果数据对你比较重要建议自行实现大数据量的写入队列

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!