PHP flock()替代 [英] PHP flock() alternative

查看:75
本文介绍了PHP flock()替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

flock() 的PHP文档页面指示在IIS下使用它是不安全的.如果我不能在所有情况下都依赖flock,还有另一种方法可以安全地实现同一目标吗?

PHP's documentation page for flock() indicates that it's not safe to use under IIS. If I can't rely on flock under all circumstances, is there another way I could safely achieve the same thing?

推荐答案

在所有可能的情况下,没有其他方法可以安全地实现相同目标.这是由计算机系统和设计而成的,对于跨平台代码而言,这项工作并不容易.

There is no alternative available to safely achieve the same under all imaginary possible circumstances. That's by design of computer systems and the job is not trivial for cross-platform code.

如果您需要安全使用flock(),请记录您的应用程序要求.

If you need to make safe use of flock(), document the requirements for your application instead.

或者,您可以创建自己的锁定机制,但是必须确保它是原子的.这意味着,您必须测试该锁,如果不存在该锁,则在需要确保其他任何人都无法获得其间锁的同时建立该锁.

Alternatively you can create your own locking mechanism, however you must ensure it's atomic. That means, you must test for the lock and if it does not exists, establish the lock while you need to ensure that nothing else can acquire the lock in-between.

这可以通过创建一个代表锁的锁文件来完成,但前提是该锁文件不存在.不幸的是,PHP没有提供以这种方式创建文件的功能.

This can be done by creating a lock-file representing the lock but only if it does not exists. Unfortunately, PHP does not offer such a function to create a file in such a way.

或者,您可以使用 mkdir() 创建目录,并使用结果,因为创建目录时它将返回true,如果目录已经存在,则返回false.

Alternatively you can create a directory with mkdir() and work with the result because it will return true when the directory was created and false if it already existed.

这篇关于PHP flock()替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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