LOCK_NB 被忽略
运行此代码两次:
$fp = @fopen('test.test', "wb");
if (flock($fp, LOCK_NB | LOCK_EX)){
@fwrite($fp, $data);
echo 'written';
sleep(5);
}else{
echo 'skipped , ok';
}
@flock($fp, LOCK_UN);
@fclose($fp);
总是给我“书面”的输出
意味着跳过了LOCK_NB
,任何线索(在winbdows和unix上)
编辑(2012-03-29仍然没有修复):https://bugs.php.net/bug.php?id=54453&edit =3 PHP 错误 #54453
running this code twice :
$fp = @fopen('test.test', "wb");
if (flock($fp, LOCK_NB | LOCK_EX)){
@fwrite($fp, $data);
echo 'written';
sleep(5);
}else{
echo 'skipped , ok';
}
@flock($fp, LOCK_UN);
@fclose($fp);
always gives me the output of "written"
Means the LOCK_NB
is skipped , any clues (on both winbdows and unix)
EDIT (2012-03-29 still not fixed): https://bugs.php.net/bug.php?id=54453&edit=3 PHP Bug #54453
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当使用 Apache+PHP 时,我被欺骗相信 LOCK_NB 被忽略了(事实并非如此,这是浏览器在等待第一个请求完成)。
因为我使用同一个浏览器发出 2 个请求,所以浏览器在发出下一个请求之前会等待第一个调用完成(甚至忽略“Connection: close”标头)。
使用 2 个独立的浏览器(在我的例子中是 Chrome + Firefox,或服务器上的 Chrome + wget)
我的结论是 LOCK_NB 工作得很好。
如果 w+ 模式下的文件被 LOCK_EX | 锁定LOCK_NB,正在尝试另一个 LOCK_EX |同一文件上的 LOCK_NB 返回 false(预期行为)。
When using Apache+PHP I was tricked into believing LOCK_NB was ignored (it wasn't, it was the browser waiting for the first request to finish).
Because I was making 2 requests with the same browser, the browser was waiting for the first call to finish before making the next one (even ignoring a "Connection: close" header).
Using 2 separate browsers (in my case Chrome + Firefox, or Chrome + wget on the server)
I concluded LOCK_NB worked just fine.
If a file in w+ mode was locked with LOCK_EX | LOCK_NB, attempting another LOCK_EX | LOCK_NB on the same file returned false (the intended behaviour).
LOCK_NB 仅在以下情况下起作用:
LOCK_SH
锁定文件并且执行LOCK_EX|LOCK_NB
LOCK_EX
锁定并且执行LOCK_SH|LOCK_NB
如果满足以下条件,则忽略 LOCK_NB:
LOCK_EX
锁定,并且您执行LOCK_EX|LOCK_NB
LOCK_SH
锁定然后你做了一个LOCK_SH|LOCK_NB
我猜这是一个错误?或者他们需要制作一个 LOCK_NB2 ?
我将此作为错误报告给 PHP.NET 。
编辑(2012-03-22 仍未修复):
https://bugs.php.net/bug.php?id=54453& ;编辑=3
PHP 错误 #54453
LOCK_NB works only when :
LOCK_SH
and you do aLOCK_EX|LOCK_NB
LOCK_EX
and you do aLOCK_SH|LOCK_NB
LOCK_NB is ignored if :
LOCK_EX
and you do aLOCK_EX|LOCK_NB
LOCK_SH
and you do aLOCK_SH|LOCK_NB
i guess this is a bug ? or they need to make a LOCK_NB2 ?
i reported this as a bug to PHP.NET .
EDIT (2012-03-22 still not fixed):
https://bugs.php.net/bug.php?id=54453&edit=3
PHP Bug #54453