Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long computation in php results in 503 error

I have script that takes long time to execute. In a result server responds in 503 error. How can I set longer execution time?

In my PHP script I set:

set_time_limit(0);
ignore_user_abort(true);
like image 780
pixel Avatar asked Dec 03 '25 07:12

pixel


2 Answers

This question is very similar to PHP Background Processes. So read that; In addition,

First you need to change the time limit in php.ini; you cannot change it in the script itself. Or in .htaccess using php_value

Second, you may be you want to modify how you interact with user more friendly so that the page uses an update; you can explore ajax; http-refresh; multipart pages for different approaches. If you are doing long calculations perhaps you can launch them in the background and update a database, retrieve the results later.

like image 147
Ahmed Masud Avatar answered Dec 05 '25 19:12

Ahmed Masud


Despite what time-limit you set in the script, a web request might time out earlier because the client gives up, or because any proxy server in the middle gives up, or possibly if the apache server is configured to give up.

As the other answerer mentioned; the maximum_execution_time variable might help, but it would be better if you didn't leave the visitor waiting with a long delay.

Consider using a pattern that shows feedback:

  1. User initiates action; eg POST request
  2. PHP Script returns immediately with a page containing javascript or an iframe.
  3. The javascript/inner frame calls the long-running script.
  4. Every few seconds the javascript updates the user on how the script is going, using a LONGPOLL-like mechanism.
  5. On completion or error, the javascript notifies and redirects to the next page.
like image 28
MrTrick Avatar answered Dec 05 '25 20:12

MrTrick