How do you prevent a function from executing for too long? In PHP you can use the PCNTL clock signal + exception to achieve.
The code is as follows:
DECLARE (ticks = 1); function A () { sleep (ten); echo "a finishi\n";} Function B () { echo "stop\n";} Function C () { usleep (100000);} function sig () { throw new Exception;} try{ pcntl_alarm (1); Pcntl_signal (SIGALRM, "sig"); A (); Pcntl_alarm (0);} catch (Exception $e) { echo "timeout\n";} b (); a (); B ();
The principle is to set a clock signal before the function executes, and if the function executes more than the specified time, the signal is triggered and the signal processing function throws an exception and is captured by the outer code.
This jumps out of the original function and executes the following code. If the function is within a specified time, the clock signal is not triggered, and the clock signal is cleared at the end of the function and no exception is thrown.
http://www.bkjia.com/PHPjc/440158.html www.bkjia.com true http://www.bkjia.com/PHPjc/440158.html techarticle How do you prevent a function from executing for too long? In PHP you can use the PCNTL clock signal + exception to achieve. The code is as follows: Declare (ticks = 1); function A () {sleep]; echo "A finishi\ ...