LOCK_NB在群中是什么意思? [英] What does LOCK_NB mean in flock?

查看:84
本文介绍了LOCK_NB在群中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LOCK_NB在PHP flock命令中是什么意思?

What does LOCK_NB mean in the PHP flock command?

推荐答案

LOCK_NB表示无阻塞.

通常,当您尝试锁定文件时,您的PHP脚本执行将停止.对flock()的调用然后阻止将其恢复.直到删除对已访问文件的并发锁定为止.

Usually when you try to lock a file, your PHP script execution will stop. The call to flock() then blocks it from resuming. It does so until a concurrent lock on the accessed file is removed.

通常,您的进程是唯一尝试锁定文件的进程,因此对flock的阻塞调用实际上会立即返回.只有两个进程锁定同一个文件时,它们中的一个才会被暂停.

Mostly your process is the only one trying to lock the file, so the blocking call to flock actually returns instantly. It's only if two processes lock the very same file, that one of them will be paused.

但是LOCK_NB标志将使flock()在任何情况下都立即返回 .在该设置中,您必须检查返回的状态以查看您是否实际获得了锁.例如:

The LOCK_NB flag however will make flock() return immediately in any case. In that setting you have to check the returned status to see if you actually aquired the lock. As example:

while ( ! flock($f, LOCK_NB) ) {
    sleep(1);
}

或多或少会模拟正常阻塞调用的行为.当然,这样做的目的是在文件仍然被另一个进程锁定的同时做其他有意义的事情(不仅仅是等待).

Would more or less emulate the behaviour of the normal blocking call. The purpose of course is to do something else / meaningful (not just wait) while the file is still locked by another process.

这篇关于LOCK_NB在群中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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