php 睡眠时间是否计入执行时间限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/740954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:42:50  来源:igfitidea点击:

Does sleep time count for execution time limit?

phpsleepexecutionmax

提问by caw

I have two questions concerning the sleep()function in PHP:

我有两个关于sleep()PHP 函数的问题:

  1. Does the sleep time affect the maximum execution time limit of my PHP scripts? Sometimes, PHP shows the message "maximum execution time of 30 seconds exceeded". Will this message appear if I use sleep(31)?

  2. Are there any risks when using the sleep()function? Does it cost a lot of CPU performance?

  1. 睡眠时间是否会影响我的 PHP 脚本的最大执行时间限制?有时,PHP 会显示消息“超出最大执行时间 30 秒”。如果我使用,会出现此消息sleep(31)吗?

  2. 使用该sleep()功能是否有任何风险?是否会消耗大量 CPU 性能?

回答by Samuel

You should try it, just have a script that sleeps for more than your maximum execution time.

您应该尝试一下,只要有一个睡眠时间超过最大执行时间的脚本即可。

<?php
  sleep(ini_get('max_execution_time') + 10);
?>

Spoiler: Under Linux, sleeping time is ignored, but under Windows, it counts as execution time.

剧透:在 Linux 下,睡眠时间被忽略,但在 Windows 下,它算作执行时间。

回答by cletus

It only affects script time not system calls like sleep(). There is apparently a bug where on Windows sleep() is included. Max execution time is about real-time, not CPU time or anything like that. You can change it however:

它只影响脚本时间,而不影响像 sleep() 这样的系统调用。显然有一个错误,其中包含 Windows sleep()。最大执行时间是实时的,而不是 CPU 时间或类似的时间。但是,您可以更改它:

  • max_execution_timedirective in your php.ini. This is a global setting;
  • Using ini_set()with the above directive. This will change it only for the currently executing script only for that execution;
  • set_time_limit(): also a local change.
  • php.ini 中的max_execution_time指令。这是一个全局设置;
  • ini_set()与上述指令一起使用。这将仅针对当前执行的脚本更改它,仅针对该执行;
  • set_time_limit():也是本地更改。

As for the difference between the last two, I believe max_execution_time is a fixed quantity. Running:

至于后两者的区别,我认为max_execution_time是一个固定的数量。跑步:

ini_set('max_execution_time', 60);

will limit to the script to 60 seconds. If after 20 seconds you call:

将脚本限制为 60 秒。如果 20 秒后您调用:

set_time_limit(60);

the script will now be limited to 20 + 60 = 80 seconds.

脚本现在将被限制为 20 + 60 = 80 秒。

回答by karim79

From the PHP sleep() page, there's this user-contributed note:

PHP sleep() 页面中,有用户提供的注释:

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.

注意:set_time_limit() 函数和配置指令 max_execution_time 只影响脚本本身的执行时间。在确定脚本运行的最长时间时,不包括在脚本执行之外发生的活动上花费的任何时间,例如使用 system()、sleep() 函数、数据库查询等的系统调用。

回答by Calvin

Others have already covered the basics of sleep() and PHP script execution time limit, but you should also be aware of another risk when using really long sleep periods.

其他人已经涵盖了 sleep() 和 PHP 脚本执行时间限制的基础知识,但您还应该意识到使用真正长睡眠时间时的另一个风险。

Usually, when a browser sends a request to a server and does not receive any data from the server, the connection can time out. This time limit depends on the browser's configurations, but I've read that IE7 has a default value of just 30 seconds, while Firefox has a default value of 115 seconds--you can check your own configuration in Firefox by going to about:configand filtering for network.http.keep-alive.timeout(the time limit is specified in seconds).

通常,当浏览器向服务器发送请求但未从服务器接收任何数据时,连接可能会超时。这个时间限制取决于浏览器的配置,但我读到 IE7 的默认值只有 30 秒,而Firefox 的默认值是 115 秒——你可以在 Firefox 中查看你自己的配置,访问about:config并过滤network.http.keep-alive.timeout(时间限制以秒为单位)。

Edit: I had the units for network.http.keep-alive.timeout and browser.urlbar.search.timeout mixed up. It is indeed in seconds, not tenths of a second.

编辑:我将 network.http.keep-alive.timeout 和 browser.urlbar.search.timeout 的单位混淆了。确实是以秒为单位,而不是十分之一秒。

回答by v3.

a) Yes, it counts toward the time limit (so sleep(31) will trigger an error)

a) 是的,它计入时间限制(所以 sleep(31) 会触发错误)

b) It does the opposite of costing CPU performance - it lets other applications use the CPU (when an application sleeps, the CPU usage of that application will be near 0%). Aside from taking time away from the user, I can't really think of any risks of using this.

b) 它与消耗 CPU 性能相反——它让其他应用程序使用 CPU(当应用程序休眠时,该应用程序的 CPU 使用率将接近 0%)。除了占用用户的时间之外,我真的想不出使用它的任何风险。