How to lock file in PHP?

走远了吗. 提交于 2020-02-06 08:24:12

问题


I'm trying to create a PHP file, which wouldn't run if it's already running. Here's the code I'm using:

<?php

class Test {
    private $tmpfile;

    public function action_run() {
        $this->die_if_running();
        $this->run();
    }

    private function die_if_running() {
        $this->tmpfile = @fopen('.refresher2.pid', "w");

        $locked = @flock($this->tmpfile, LOCK_EX|LOCK_NB);
        if (! $locked) {
            @fclose($this->tmpfile);
            die("Running 2");
        }
    }

    private function run() {
        echo "NOT RUNNNING";
        sleep(100);
    }
}

$test = new Test();
$test->action_run();

The problem is, when I run this from console, it works great. But when I try to run it from browser, many instances can run simultaneously. This is on Windows 7, XAMPP, PHP 5.3.2. I guess OS is thinking that it's the same process and thus the functionality falls. Is there a cross-platform way to create a PHP script of this type?


回答1:


Not really anything to promising. You can't use flock for that like this.

You could use system() to start another (php) process that does the locking for you. But drawbacks:

  • You need to do interprocess communication. Think about a way how to tell the other program when to release the lock etc. You can use stdin for messenging und use 3 constants or something. In this case it's still rather simple
  • It's bad for performance because you keep creating processes which is expensive.

Another way would be to start another program that runs all the time. You connect to it using some means of IPC (probably just use a tcp channel because it's cross-platform) and allow this program to manage file acces. That program could be a php script in an endless loop as well, but it will probably be simpler to code this in Java or another language that has multithreading support.

Another way would be to leverage existing ressources. Create a dummy database table for locks, create an entry for the file and then do table-row-locking.

Another way would be not to use files, but a database.




回答2:


I had a similar problem a while ago. I needed to have a counter where the number returned was unique. I used a lock-file and only if this instance was able to create the lock-file was it allowed to read the file with the current number.

Instead of counting up perhaps you can allow the script to run. The trick is to let try a few times (like 5) with a small wait/sleep in between.

function GetNextNumber()
{
  $lockFile = "lockFile.txt";
  $lfh = @fopen($lockFile, "x");
  if (!$lfh)
  {
    $lockOkay = false;
    $count = 0;
    $countMax = 5;

    // Try ones every second in 5 seconds
    while (!$lockOkay & $count < $countMax) 
    {
      $lfh = @fopen($lockFile, "x");
      if ($lfh)
      {
        $lockOkay = true;
      }
      else
      {
        $count++;
        sleep(1);
      }
    }
  }

  if ($lfh)
  {
    $fh = fopen($myFile, 'r+') or die("Too many users. ");
    flock($fh, LOCK_EX);
    $O_nextNumber = fread($fh, 15);
    $O_nextNumber = $O_nextNumber + 1;
    rewind($fh);
    fwrite($fh, $O_knr);
    flock($fh, LOCK_UN);  
    fclose($fh);

    unlink($lockFile); // Sletter lockfilen
  }

  return $O_nextNumber;
}


来源:https://stackoverflow.com/questions/4906988/how-to-lock-file-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!