dregvw1801 2019-02-14 10:59
浏览 118

如果PHP脚本已经运行,请不要再次运行它

I am reading data from the sheet and want to implement singleton in my process so that I can run read function, one at a time. but i got to know that i cant do that with singleton so how can i do that without using mutex or semaphore.

I have written a code for the same which calls the class and set a static variable and then call the function to check if it has the class, so if the class is there then don't run the read function.

class DataParser extends config {

   private static $_instance = false;

   public static
   function getInstance() {
    if (self::$_instance == false) {
     print_r("expression");
     self::$_instance = true;
     return self::$_instance;

    }
    return false;
   }

   function __construct($params) {}
}

$dataParser = new DataParser($confData);
$p = DataParser::getInstance();
if ($p) {
   $res = $dataParser - > read();

}

I want to run the read function one at a time, if one read is running then the other read will not run using php dont want to use mutex or semaphore

  • 写回答

1条回答 默认 最新

  • duanfei8399 2019-02-14 11:15
    关注

    Are you using a database in your app? Is the reader task running in a distributed environment or always on the same server? I suggest looking into advisory locking. Both MySQL and PHP supports it.

    It would look something like this using an exclusive lock on file in PHP.

    class DataParser extends config {
    
       private static $_instance = false;
    
       public static
       function getInstance() {
        if (self::$_instance == false) {
         print_r("expression");
         self::$_instance = true;
         return self::$_instance;
    
        }
        return false;
       }
    
       function __construct($params) {}
    }
    
    
    $dataParser = new DataParser($confData);
    $p = DataParser::getInstance();
    
    $fp = fopen("/tmp/.lock", "r+");
    
    if ($p && flock($fp, LOCK_EX)) {  
        $res = $dataParser->read();
        flock($fp, LOCK_UN);    
    } else {
        // Skip silently or do something else?
    }
    
    fclose($fp);
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码