LOCK_NB在群中是什么意思? [英] What does LOCK_NB mean in flock?
问题描述
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屋!