PHP文件写入线程问题 [英] PHP file write threading issues

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

问题描述

在PHP网页中,我以写模式打开文件,读取并删除第一行并关闭文件。 (该文件有1000行)

现在,问题是,如果有100个用户连接到该页面,所有将以写入模式打开该文件而不是在删除第一行后尝试写入。
在这种情况下是否会出现死锁?

有关您的信息,我们正在使用IIS服务器和PHP5的Windows服务器。

感谢您的帮助。

使用 flock 只允许一个用户访问文件。

但是别忘了通过 fclose 释放您的文件锁定



更新。考虑这个代码:

pre $ <?php
$ start = time();
echo'Started at'。$ start。'< br />';
$ filename ='D:\Kindle\books\Brenson_Teryaya_nevinnost__Avtobiografiya_66542.mobi';
$ fp = fopen($ filename,'w +')或死('无法访问'。$ filename);

if(flock($ fp,LOCK_EX)){
echo'文件被锁定在'.time()。'。授予专用权限写入< br />';
}
else {
echo'文件被其他用户锁定< br />';
}
sleep(3);
flock($ fp,LOCK_UN);
echo'文件锁定在'.time()。'< br />'上被释放。
fclose($ fp);
$ end = time();
echo'Finished at'。$ end。'< br />';
echo'Proccessing time'。($ end - $ start)。'< br />';

运行这段代码两次(它锁定文件3秒钟,让我们考虑一下我们的手动脚本运行为异步)。您会看到如下所示:



第一个实例:


  • 文件被锁定授予独占访问权
  • 文件锁已在 1302788741 $ b

    第二:


    • 文件被锁定在 1302788741 。授予独占访问权
    • 文件锁已在1302788744发布



    注意,等待第一个释放文件锁定。

    如果它不符合您的要求,那么...尝试发明其他解决方案,如:
    用户可以读取文件,然后编辑一行并将其保存为临时文件,其他用户保存临时文件等等,一旦所有用户都释放了文件锁定,就可以将所有临时文件的补丁组合成新的文件(使用保存文件mtime定义哪个文件应该与其他文件分层)...这样的东西....也许...我不是这类任务的专家,不幸的是我只是假设你完成了这个任务。


    In a PHP webpage, Im opening a file in write mode, reading and than deleting the first line and closing the file. (The file has 1000's of lines)

    Now, what the problem is, if there are like 100 users connected to that page, all will be opening that file in write mode and than try to write it after deleting the first line. Will there be any deadlocks in this situation?

    For your information, we are using Windows server with IIS server and PHP5.
    Thanks in advance for your help.

    解决方案

    Use flock to grant access to file only for one user at a time.

    But don't forget release your file lock by fclose

    Update. Consider this code:

    <?php
    $start = time();
    echo 'Started at '.$start.'<br />';
    $filename = 'D:\Kindle\books\Brenson_Teryaya_nevinnost__Avtobiografiya_66542.mobi';
    $fp = fopen($filename, 'w+') or die('have no access to '.$filename);
    
    if (flock($fp, LOCK_EX)) {
        echo 'File was locked at '.time().'. Granted exclusive access to write<br />';
    }
    else {
        echo 'File is locked by other user<br />';
    }
    sleep(3);
    flock($fp, LOCK_UN);
    echo 'File lock was released at '.time().'<br />';
    fclose($fp);
    $end = time();
    echo 'Finished at '.$end.'<br />';
    echo 'Proccessing time '.($end - $start).'<br />';
    

    Run this code twice (it locks file for 3 seconds, so let's consider our manual script run as asynchronous). You will see something like this:

    First instance:

    • File was locked at 1302788738. Granted exclusive access to write
    • File lock was released at 1302788741

    Second:

    • File was locked at 1302788741. Granted exclusive access to write
    • File lock was released at 1302788744

    Notice, that second instance waited for first to release file lock.

    If it does not comply your requirements, well... try to invent other solution like: user can read file, then he edit one line and save it as temporary, other user saves his temporary file and so on and once you have all users released file lock, you compose new file as patch of all temporary files on each other (use save files mtime to define which file should stratify other one)... Something like this.... maybe... I'm not the expert in this kind of tasks, unfortunately - just my assumption on how you can get this done.

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

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