LOCK_NB被忽略 [英] LOCK_NB Ignored

查看:121
本文介绍了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

解决方案

在使用Apache + PHP时,我被误认为是LOCK_NB被忽略了(不是,浏览器在等待第一个请求完成)./p>

因为我在同一浏览器上发出了2个请求,所以浏览器在完成下一个请求之前一直在等待第一个调用完成(甚至忽略了"Connection:close"标头).

使用2个单独的浏览器(在我的情况下为Chrome + Firefox,或服务器上的Chrome + wget) 我得出结论LOCK_NB正常工作.

如果w +模式下的文件被LOCK_EX锁定| LOCK_NB,正在尝试另一个LOCK_EX |同一文件上的LOCK_NB返回false(预期行为).

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

解决方案

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被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆