Home > Backend Development > PHP Tutorial > Set timeout limit for function in pcntl_alarm in php_PHP tutorial

Set timeout limit for function in pcntl_alarm in php_PHP tutorial

WBOY
Release: 2016-07-13 10:50:11
Original
926 people have browsed it

The principle is to set a clock signal before the function is executed. If the execution of the function exceeds the specified time, the signal will be triggered, and the signal processing function (sig) will throw an exception and be caught by the outer code. This will jump out of the execution of the original function, and then execute the following code. If the function is within the specified time, the clock signal will not be triggered. The clock signal will be cleared after the function ends, and no exception will be thrown

Go to PHP official to view the description of this function

pcntl_alarm (PHP 4 >= 4.3.0, PHP 5)

pcntl_alarm — Set an alarm clock signal for the process

Description¶int pcntl_alarm (int $seconds)

Create a timer that sends a SIGALRM signal to the process after a specified number of seconds. Each call to pcntl_alarm() cancels the previously set alarm signal.

Parameter ¶seconds The number of seconds to wait. If seconds is set to 0, the alarm signal will not be created.

Return Value¶ Returns the number of seconds remaining before the last alarm schedule (before the alarm signal is sent), or returns 0 if there is no previous alarm schedule (Translation: or the previous schedule has been completed).


Example

{ usleep(100000);
The code is as follows
 代码如下 复制代码

declare(ticks = 1);
function a()
{
    sleep(10);
    echo "a finishin";
}
function b()
{
    echo "Stopn";
}
function c()
{
    usleep(100000);
}
 
function sig()
{
    throw new Exception;
}
 
try
{
    //设置一个闹钟信号为一秒钟执行一次
    pcntl_alarm(1);
    //安装闹钟信号,并绑定callback
    pcntl_signal(SIGALRM, "sig");
    a();
    //取消闹钟信号
    pcntl_alarm(0);
}
catch(Exception $e)
{
    echo "timeoutn";
}
 
b();
a();
b();

Copy code


declare(ticks = 1); function a()

{

Sleep(10);

echo "a finishin";

}

function b()

{
 代码如下 复制代码
# cd /usr/local/src/php-5.2.6/ext/pcntl
# phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make && make install
echo "Stopn";

}

function c()
}

function sig()

{ } try { //Set an alarm signal to be executed once per second pcntl_alarm(1); //Install the 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 extension installation The pcntl extension can support multi-threaded operations of PHP. Add --enable-pcntl to the configure prompt after you originally needed to recompile PHP In order to save trouble, directly compile the calculation bird.
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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632645.htmlTechArticleThe principle is to set a clock signal before the function is executed. If the execution of the function exceeds the specified time, the signal will be Triggered, the signal processing function (sig) will throw an exception, which is generated by the outer layer...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template