如何保证多个进程写文件成功

版权声明:珞羽飘凌 https://blog.csdn.net/lingluo110700/article/details/85635053
function write_file($filename, $content)
{
    $lock = $filename . '.lck';
    $write_length = 0;
    while(true) {
        if( file_exists($lock) ) {
            usleep(100);
        } else {
            touch($lock);
            $write_length = file_put_contents($filename, $content, FILE_APPEND);
            break;
        }
    }
    if( file_exists($lock) ) {
        unlink($lock);
    }
    return $write_length;
}

猜你喜欢

转载自blog.csdn.net/lingluo110700/article/details/85635053