【问题标题】:Execute the scripts at the same time if-else problem同时执行脚本if-else问题
【发布时间】:2011-07-22 13:59:30
【问题描述】:

我现在不能使用flock(服务器限制),所以我正在创建一个替代文件锁定系统。这是我的代码。

$dir = "C:\\wamp\\www\\test\\";
$files = scandir($dir);

for($i=0; $i<count($files); $i++) 
{   
    if(substr(strrchr($files[$i],'.csv'),-4) == '.csv')
    {           
        echo "File ".$files[$i]." is a csv"."</br>";
        if (file_exists("$dir$files[$i].lock"))
        {
            echo $files[$i]." has lock in place"."</br>";
            $i++;
        }
        else
        {
            if($file_handle = fopen("$dir$files[$i]", "rb"))
            {
                $file_lock_handle = fopen("$dir$files[$i].lock", "w");
                echo "Setting Lock"."</br>";

                //Do Logic

                fclose($file_handle);
                fclose($file_lock_handle);          

                sleep(3);
                unlink("$dir$files[$i].lock");
            }       
        }
    }
    else
    {
        //Do nothing
    }   
}

如果我并排运行这些脚本。它在执行第二个脚本之前等待第一个脚本完成。我怎样才能同时运行它们?即如果存在锁,我希望它跳过该文件并转到下一个。

【问题讨论】:

    标签: php concurrency if-statement file-locking


    【解决方案1】:

    【讨论】:

    • 非常感谢,正是我想要做的!
    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多