开发者

What does LOCK_NB mean in flock?

开发者 https://www.devze.com 2023-02-21 11:02 出处:网络
What does LOCK_NB mean in the PHP开发者_C百科 flock command?LOCK_NB means non-blocking. Usually when you try to lock a file, your PHP script execution will stop. The call to flock() then blocks it fr

What does LOCK_NB mean in the PHP开发者_C百科 flock command?


LOCK_NB means non-blocking.

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.

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消