Set timeout for PHP functions _php Tutorial

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

  • 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.