flock lock ex php,php – flock有可能用LOCK_EX返回false吗?
標簽:php
By default, this function will block until the requested lock is
acquired
在下面我發現了以下示例代碼:
$fp = fopen("/tmp/lock.txt", "r+");
if (flock($fp, LOCK_EX)) { // acquire an exclusive lock
ftruncate($fp, 0); // truncate file
fwrite($fp, "Write something here\n");
fflush($fp); // flush output before releasing the lock
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't get the lock!";
}
fclose($fp);
?>
但有沒有任何情況下腳本實際上會返回“無法鎖定!”?我以為它一直等到文件lock.txt解鎖.如果文件永遠不會被解鎖,那么腳本會永遠等待,對吧?
此外,我發現這個答案解釋了unix上的獨占鎖和共享鎖之間的區別:https://stackoverflow.com/a/11837714/2311074
這四個規則是否也適用于PHP中的flock(例如“如果已經存在一個或多個共享鎖,則無法獲得獨占鎖”)?
解決方法:
是的,阻止flock可以返回false.
如果提供的資源不支持鎖定,則會發生這種情況
樣本甚至在文檔中提供
May only be used on file pointers returned by fopen() for local files,
or file pointers pointing to userspace streams that implement the
streamWrapper::stream_lock() method.
因此,如果您嘗試鎖定ftp或http資源,您將得到錯誤,同樣會用于某些包裝器,例如zlib或phar.
flock() is not supported on antiquated filesystems like FAT and its
derivates and will therefore always return FALSE under these
environments.
標簽:php
來源: https://codeday.me/bug/20190627/1308714.html
總結
以上是生活随笔為你收集整理的flock lock ex php,php – flock有可能用LOCK_EX返回false吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 14 代码分割之lazy:Suspens
- 下一篇: 室友转行软件测试,月薪1.4万,天天摸鱼