PHP pcntl_alarm to set timeout limits for functions _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.