简体   繁体   English

PHP max_execution_time如何工作?

[英]How does PHP max_execution_time work?

I have few doubts about maximum execution time set in php.ini. 我对php.ini中设置的最长执行时间几乎没有疑问。

Assuming max_execution_time is 3 minutes, consider the following cases: 假设max_execution_time为3分钟,请考虑以下情况:

  1. I have a process which will end in 2 minutes. 我有一个过程,将在2分钟后结束。

    But it's in a loop and it should work 5 times. 但它处于一个循环中,应该可以工作5次。 So it become 10 minutes. 所以它变成了10分钟。

    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. PHP函数只打印数据,只需2分钟。

    But the query execution is taking 5 minutes. 但查询执行需要5分钟。

    Will the script run without error? 脚本会运行没有错误吗? Why? 为什么?

  3. My single php process itself take 5 minutes. 我的单个php进程本身需要5分钟。

    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. 我不想只是增加时间限制和内存限制。

The rules on max_execution_time are relatively simple. max_execution_time的规则相对简单。

  • 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. 准备请求,准备上传文件,Web服务器等事务所需的时间不计入执行时间。

  • 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 . 因此,在第一种和第二种情况下,脚本将以超时错误终止,因为这是max_execution_time的已定义行为。

  • External system calls using exec() and such do not count towards the execution time except on Windows. 使用exec()外部系统调用除了Windows之外不会计入执行时间。 ( Source ) That means that you could run a external program that takes longer than max_execution_time . 来源 )这意味着你可以运行一个比max_execution_time更长的外部程序。

  • When called from the command line, max_execution_time defaults to 0 . 从命令行调用时, max_execution_time默认为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. 如果是这样,那么通常是由于一个循环,其中变量未被设置,并且先前保留的内存未被正确释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM