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