Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PHP max_execution_time work?

Tags:

php

I have few doubts about maximum execution time set in php.ini.

Assuming max_execution_time is 3 minutes, consider the following cases:

  1. I have a process which will end in 2 minutes.

    But it's in a loop and it should work 5 times. So it become 10 minutes.

    Will the script run properly without showing error for timeout? Why?

  2. PHP function just prints the data and it will take only 2 minutes.

    But the query execution is taking 5 minutes.

    Will the script run without error? Why?

  3. My single php process itself take 5 minutes.

    But am calling the script from command line.

    Will it work properly? Why?

  4. How are memory allowed and execution time related?

    If execution time for a script is very high

    But it returns small amount of data

    Will it affect memory or not? Why?

I want to learn what is happening internally, that is why am asking these. I don't want to just increase time limit and memory limit.

like image 543
zod Avatar asked Nov 18 '10 22:11

zod


People also ask

What is the best value for max_execution_time in PHP?

max_execution_time is set to 60 which is too low. A minimum execution time of 150 seconds is recommended to give the migration process the best chance of succeeding.

What is the maximum value for max_execution_time?

One of these rules, Max_Execution_Time, defines the maximum time a script can run for. By default, this is set to 30 seconds. If a script runs for longer than 30 seconds, it is automatically stopped, and an error is reported. You may wish to extend this time limit if you need to run large scripts on your server.

How do you increase the maximum execution time of 30 seconds exceeded?

Change the execution time by adding the php_value max_execution_time 300 code—just as outlined above. Save changes and upload the edited file to your server to overwrite the old one. You can also increase the max_execution_time value by editing the wp-config. php file.

How can I limit time in PHP?

Use PHP inbuilt function set_time_limit(seconds) where seconds is the argument passed which is the time limit in seconds. It is used, when the user changes the setting outside the php. ini file. The function is called within your own PHP code.


1 Answers

The rules on max_execution_time are relatively simple.

  • Execution time starts to count when the file is interpreted. Time needed before to prepare the request, prepare uploaded files, the web server doing its thing etc. does not count towards the execution time.

  • The execution time is the total time the script runs, including database queries, regardless whether it's running in loops or not. So in the first and second case, the script will terminate with a timeout error because that's the defined behaviour of max_execution_time.

  • External system calls using exec() and such do not count towards the execution time except on Windows. (Source) That means that you could run a external program that takes longer than max_execution_time.

  • When called from the command line, max_execution_time defaults to 0. (Source) So in the third case, your script should run without errors.

  • Execution time and memory usage have nothing to do with each other. A script can run for hours without reaching the memory limit. If it does, then often due to a loop where variables are not unset, and previously reserved memory not freed properly.

like image 142
Pekka Avatar answered Sep 17 '22 19:09

Pekka