Detecting a timeout for a block of code in PHP

后端 未结 9 1626
[愿得一人]
[愿得一人] 2021-02-02 16:44

Is there a way you can abort a block of code if it\'s taking too long in PHP? Perhaps something like:

//Set the max time to 2 seconds
$time = new TimeOut(2);
$ti         


        
9条回答
  •  逝去的感伤
    2021-02-02 17:07

    Here is my way to do that. Thanks to others answers:

     self::$timeout) {
             throw new \RuntimeException($error ?? 'You code took too much time.');
          }
       }
    
       public static function step()
       : void
       {
          usleep(1);
       }
    }
    

    Then you can try like this:

      getMessage(); // 'You code is heavy. Sorry.'
      }
    

提交回复
热议问题