duanfei1930 2016-06-16 12:50
浏览 11

cakephp 3锁定文件属性

I have an application that create many php process to read file. Sometimes process read the same file so I need to lock file when a process read It.

The application need to read only one times a file and after delete It. Sometimes two process read the same file and this is a big problem.

I have create many process that every one loop through a folder, read all file, and delete it. I don't want that two process read the same file, but the problem is that the process works simultaneously.

code of every process :

public function readAllXmlAndSave()
    $xml_files = glob(TMP . 'xml/*.xml');
    foreach($xml_files as $fileXml){
        $explStr = explode('/', $fileXml);
        $filename = $explStr[count($explStr) - 1];
        $path = TMP . '/xml/' . $filename;
        $file = new File($path, false);

        if($file->exists()){
           $string = $file->read();
           //some work
           $file->close();
           $file->delete();
        }
    }
}

I have create a simple script to understand how lock file works into cakephp 3 but seems that property lock doesn't work as I aspected.

code

$path = TMP . '/xml/test.xml'; //inside there is "HELLO"
$file = new File($path, false);

$string = $file->read();
echo($string);
$file->lock = true;

$string2 = $file->read();
echo($string2);

$string3 = $file->read();
echo($string3);

I aspected that only the first echo work not the other.. Why I get always:

HELLO
HELLO
HELLO 

and not only the first echo?

HELLO
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大