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

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

WBOY
WBOYOriginal
2016-06-06 20:24:561106browse

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

<code>    file_put_contents("a.txt","a",FILE_APPEND);
</code>

?>

回复内容:

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

<code>    file_put_contents("a.txt","a",FILE_APPEND);
</code>

?>

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

<code class="php">file_put_contents("a.txt", "a", FILE_APPEND | LOCK_EX);</code>
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