同时写入同一个文件 [英] Writing to same file at the very same time

查看:141
本文介绍了同时写入同一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我知道在这个网站上也有类似的问题,但是这个问题并没有提供答案,不是我所需要的。



我正在构建基于平面文件的CMS。
如果是,例如:


  1. <2>,3,10 .....在附加模式下的fwrite请求在同一时间来到同一个php文件contact_form_messages_container.php?
  2. <2>,3,10 .....在w模式下的fwrite请求来到相同的PHP文件,其中包含特定页面访问的简单nubmer,同时再次?

我知道关于flock()函数,但它可能发生两个或更多的flock()请求来同时...有谁知道解决这个问题?我唯一想到的就是在一段时间内使用脚本,直到目标文件变得不平衡,但是我不知道它是否有效,在哪里以及如何执行?
有没有人有这个问题的实际经验?
在此先感谢!

解决方案 函数用于处理多个并发的读写器进行文件操作;默认情况下, flock()可以挂起一个进程,直到获得兼容的锁(即共享或独占)。一旦获得,锁可以在稍后被释放以允许其他进程在文件上操作;当文件关闭或进程结束时,锁隐式释放。



除非您的文件位于NFS上,否则我非常怀疑您会遇到两种冲突锁将会同时给出。



下面举例说明使用 flock()

  //打开文件(注意不要使用w模式)
$ f = fopen('file.txt','r +');
//获得一个exlusive锁(可以暂停进程)
if(flock($ f,LOCK_EX)){
//这个进程现在拥有唯一的独占锁

//修改文件

//释放锁
flock($ f,LOCK_UN);

//不要在$ f上执行任何写操作
fclose($ f);

使用 LOCK_NB 标志和 LOCK_EX LOCK_SH 将阻止进程挂起;如果调用返回 false ,则可以传递第三个参数来确定进程是否被挂起(Windows不支持)。

  if(false === flock($ f,LOCK_EX | LOCK_NB,$ willblock)){
if($ wouldblock){
//如果不暂停进程
} else {
//由于错误
}
}


Ok, I know that there's been similar questions on this site about this problem, but none of this questions and provided answers isn't exactly what I need.

I'm building flat-file based CMS. What if, for example:

  1. 2, 3, 10..... fwrite in appending mode requestes come to same php file "contact_form_messages_container.php" at the same time?

  2. 2, 3, 10..... fwrite in "w" mode requestes come to same php file which holds the simpley nubmer of specific page visits, again at the same time?

I know about flock() function, but it could happen two or more flock() requests comes on the same time... Does anyone knows solution to this problem? Only thing I have on my mind is usleep()-ing the script using while looop for some amount of time, until the target file becomes availibile, but I do not have idea if it works, where and how to perform this? Does anyone have practical expirience with this issue? Thanks in advance!

解决方案

The flock() function is designed to handle multiple concurrent readers and writers for file operations; by default flock() may suspend a process until a compatible lock can be obtained (i.e. shared or exclusive). Once obtained, a lock can later be released to allow other processes to operate on the file; locks are released implicitly when the file is closed or the process ends.

Unless your files are on NFS, I highly doubt you will ever run into a situation whereby two conflicting locks would be given simultaneously.


The following illustrates a basic example of using flock():

// open the file (take care to not use "w" mode)
$f = fopen('file.txt', 'r+');
// obtain an exlusive lock (may suspend the process)
if (flock($f, LOCK_EX)) {
    // this process now holds the only exclusive lock

    // make changes to the file

    // release the lock
    flock($f, LOCK_UN);
}
// don't perform any write operation on $f here
fclose($f);

Using the LOCK_NB flag together with LOCK_EX or LOCK_SH will prevent the process from being suspended; if the call returns false a third parameter can be passed to determine whether the process would have been suspended (not supported on Windows).

if (false === flock($f, LOCK_EX | LOCK_NB, $wouldblock)) {
    if ($wouldblock) {
        // the lock could not be obtained without suspending the process
    } else {
        // the lock could not be obtained due to an error
    }
}

这篇关于同时写入同一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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