ab测试 - php这串代码为什么并发时有的会写不进去

WBOY
Release: 2016-06-06 20:24:56
Original
1090 people have browsed it

拿AB压测了下 -n1000 -c10 每次写进的数量都不一样。

 file_put_contents("a.txt","a",FILE_APPEND); 
Copy after login
Copy after login

?>

回复内容:

拿AB压测了下 -n1000 -c10 每次写进的数量都不一样。

 file_put_contents("a.txt","a",FILE_APPEND); 
Copy after login
Copy after login

?>

因为并发状态下,用户1进来读取到的a.txt是100个字符,如果用2进来读取到的a.txt也是100个字符,则用户1会在101位置处写入,用户2也会在此位置写入,所以结果可能会比想要的结果少几个字符,所以每次数量都不一样,可以在写文件时加锁,如下

file_put_contents("a.txt", "a", FILE_APPEND | LOCK_EX);
Copy after login
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 admin@php.cn
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!