我如何能确保我通过Apache运行PHP脚本只有一个实例? [英] How can I ensure I have only one instance of a PHP script running via Apache?
问题描述
我有我作为一个使用的index.php
脚本后提交URL。该脚本克隆一个目录,并建立,可能需要一些工作的项目。我想避免具有该脚本并行运行超过一次。
I have an index.php
script that I use as a post-commit URL on a Google Code site. This script clones a directory and builds a project that may take some work. I want to avoid having this script running more than one time in parallel.
有一个机制,我可以用它来避免执行该脚本,如果再有一个就是已在会话?
Is there a mechanism I can use to avoid executing that script if another one is already in session?
推荐答案
您可以使用涌向与 LOCK_EX
来的一个文件获得独占锁定。
You can use flock with LOCK_EX
to gain an exclusive lock on a file.
例如:
<?php
$fp = fopen('/tmp/php-commit.lock', 'r+');
if (!flock($fp, LOCK_EX | LOCK_NB)) {
exit;
}
// ... do stuff
fclose($fp);
?>
有关5.3.2后,您需要使用手动解除锁定的PHP版本
羊群($ FP,LOCK_UN);
For PHP versions after 5.3.2 you need to manually release the lock using flock($fp, LOCK_UN);
这篇关于我如何能确保我通过Apache运行PHP脚本只有一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!