In php, pcntl_alarm sets the timeout limit for the function.

Source: Internet
Author: User
The principle is to set a clock signal before the function is executed. if the function is executed more than the specified time, the signal is triggered. the signal processing function (sig) throws an exception and is captured by the outer code.

The principle is to set a clock signal before the function is executed. if the function execution exceeds the specified time, the signal will be triggered, and the signal processing function (sig) will throw an exception, captured by the outer code. In this way, the execution of the original function is jumped out, and the following code is executed. If the function does not trigger the clock signal within the specified time period, and the clock signal is cleared after the function ends, no exception is thrown.

Go to the php official website to view this function description

Pcntl_alarm (PHP 4> = 4.3.0, PHP 5)

Pcntl_alarm-set an alarm signal for the process

Description: optional int pcntl_alarm (int $ seconds)

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

The number of seconds that the ¶ seconds parameter is waiting. If seconds is set to 0, the alarm signal is not created.

Return value: the number of seconds remaining for the last alarm scheduling (sent from the alarm signal), or 0 if no alarm scheduling is available or the previous scheduling is completed.

The instance code is as follows:

  1. Declare (ticks = 1 );
  2. Function ()
  3. {
  4. Sleep (10 );
  5. Echo "a finishin ";
  6. }
  7. Function B ()
  8. {
  9. Echo "Stopn ";
  10. }
  11. Function c ()
  12. {
  13. Usleep (100000 );
  14. }
  15.  
  16. Function sig ()
  17. {
  18. Throw new Exception;
  19. }
  20.  
  21. Try
  22. {
  23. // Set an alarm signal to be executed once per second
  24. Pcntl_alarm (1 );
  25. // Install the alarm signal and bind the callback
  26. Pcntl_signal (SIGALRM, "sig ");
  27. A ();
  28. // Cancel the alarm signal
  29. Pcntl_alarm (0 );
  30. }
  31. Catch (Exception $ e)
  32. {
  33. Echo "timeoutn ";
  34. }
  35. B ();
  36. A ();
  37. B ();
Appendix: pcntl extension installation

The pcntl extension can support php multi-thread operations. the configrue prompt added -- enable-pcntl after PHP needs to be re-compiled. the code is as follows:

  1. # Cd/usr/local/src/php-5.2.6/ext/pcntl
  2. # Phpize
  3. #./Configure -- with-php-config =/usr/local/php/bin/php-config
  4. # Make & make install
Add pcntl. so to php. ini. OK

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.