gpt4 book ai didi

PHP 锁定不起作用。不明白为什么

转载 作者:行者123 更新时间:2023-11-29 22:32:07 25 4
gpt4 key购买 nike

大家好,我正在跟进我的问题 Acquire_lock() not working. Bot still sending requests quickly. PHP + AJAX我一直无法得到答案。

我已经简化了一切并拥有三个文件

  • abc.txt
  • 文件1.php
  • 文件2.php

全部在同一目录中并且两个php文件的内容相同:

<?php 
$x = fopen("/var/www/abc.txt", "w");
if (flock($x, LOCK_EX|LOCK_NB)) {
print "No problems, I got the lock, now I'm going to sit on it.";
while (true)
sleep(5);
} else {
print "Didn't quite get the lock. Quitting now. Good night.";
}
fclose($x);
?>

然而,当我加载其中任何一个时,我收到第二条打印消息:“没有完全锁定。现在退出。晚安。”。

有人对这个问题或前一个问题有任何想法吗?我真的对此束手无策。

一如既往地感谢您。

最佳答案

如果您希望 php 脚本不退出,那么您需要一个阻塞锁。

flock文档说你可以通过指定第三个参数来做到这一点。另外删除 LOCK_NB 可能会有所帮助。

<?php 
$x = fopen("/var/www/abc.txt", "w");
if (flock($x, LOCK_EX, 1)) {
print "No problems, I got the lock, now I'm going to sit on it.";
// wait for 5 seconds
sleep(5);
// Release the lock now so that next script is executed
flock($x , LOCK_UN);
} else {
print "Didn't quite get the lock. Quitting now. Good night.";
}
fclose($x);
?>

关于PHP 锁定不起作用。不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com