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 (SIG) 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 not triggered during the specified time, the clock signal is cleared after the function ends and no exception is thrown
To the official PHP check this function description
Pcntl_alarm (PHP 4 >= 4.3.0, PHP 5)
pcntl_alarm-setting a alarm alarm signal for a process
Description ¶int pcntl_alarm (int $seconds)
Creates a timer that sends a SIGALRM signal to the process after the specified number of seconds. Each call to Pcntl_alarm () cancels the alarm signal that was previously set.
The number of seconds that the parameter ¶seconds waits. If seconds is set to 0, the alarm signal will not be created.
The return value ¶ Returns the number of seconds left in the last alarm dispatch (from the alarm signal send), or 0 if there was no previous alarm dispatch (or before the dispatch was completed).
Instance
| |
copy code |
| !--? php declare (ticks = 1); function A () { sleep () echo "a Finishin"; } Function B () { echo "STOPN"; } Function C () { Usleep (100000); } Function sig () { throw new Exception; } Try { //set an alarm signal to execute once for one second Pcntl_alarm (1), //install alarm signal, and bind callback Pcntl_signal (SIGALRM, "sig "); A (); //Cancel alarm signal Pcntl_alarm (0); } catch (Exception $e) { echo "timeoutn"; } B (); A (); B (); |
Attached, PCNTL extended installation
The PCNTL extension can support multi-threaded operation of PHP.
Originally need to recompile PHP after configrue hint plus--enable-pcntl
For the sake of easy to compile and calculate birds directly.
| The code is as follows |
Copy Code |
# Cd/usr/local/src/php-5.2.6/ext/pcntl # phpize #./configure--with-php-config=/usr/local/php/bin/php-config # Make && make install
|
pcntl.so Add to PHP.ini OK
http://www.bkjia.com/PHPjc/632645.html www.bkjia.com true http://www.bkjia.com/PHPjc/632645.html techarticle The principle is to set a clock signal before the function executes, if the function executes more than the specified time, the signal will be triggered, the signal processing function (SIG) will throw an exception, be the outer generation ...