Timeout-Can PHP limit the function execution time?

Source: Internet
Author: User
Can PHP limit the function execution time to implement the following pseudo-code functions? {Code...} Can PHP limit the function execution time to implement the following pseudo-code functions?

foreach ($jobs as $job) {    try {        run($job);    } catch (TimeoutException $e) {        printf("Timeout: %s\n", $e->getMessage());        continue;    }}function run($job) {    // ssh, HTTP request, Connect DB etc.}

Reply content:

Can PHP limit the function execution time to implement the following pseudo-code functions?

foreach ($jobs as $job) {    try {        run($job);    } catch (TimeoutException $e) {        printf("Timeout: %s\n", $e->getMessage());        continue;    }}function run($job) {    // ssh, HTTP request, Connect DB etc.}

// Set the alarm signal processing, throwing an Exception to exit the loop declare (ticks = 1); pcntl_signal (SIGALRM, function () {throw new Exception ('process _ timeout ');}); // set the alarm, 5 seconds timeout pcntl_alarm (5); $ jobs = array_fill (0, 1000, 'job'); foreach ($ jobs as $ job) {try {run ($ job);} catch (Exception $ e) {printf ("Timeout: % s \ n", $ e-> getMessage ()); exit ;}} function run ($ job) {// ssh, HTTP request, Connect DB etc. sleep (1 );}

We recommend that you use a swooole framework.
Your code is to process a task that takes a long time.
The task in swoole submits a previous task and returns immediately. The task runs automatically in the background without worrying about the running time.
If set_time_limit () is set, the task cannot be completed.

set_time_limit()

This

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.