为什么 set_time_limit 没有按预期工作? [英] Why doesnt set_time_limit work as expected?

查看:32
本文介绍了为什么 set_time_limit 没有按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速浏览以下代码片段:

Take a quick look at the following snippet:

<?
set_time_limit(5);
sleep(30);
echo 'done';
?>

当我在我的盒子上执行这个时,脚本需要整整 30 秒,并显示完成".

When I execute this on my boxes, the script takes the full 30 seconds, and displays 'done'.

为什么?

难道它不应该在 5 秒内终止,而不是让脚本有时间显示完成"?

Shouldnt it terminate in 5 seconds and not give the script the time to display 'done'?

这不是在 CLI 模式下.Nginx + PHP_FPM.

This is NOT in CLI mode. Nginx + PHP_FPM.

有什么想法吗?

我选择将答案"放在这里,因为下面有很多好的和有效的答案.但是……这似乎是睡眠的一个特定问题.

I have opted to put the 'answer' in here as there is quite a few GOOD and VALID answers below. But... It appears to be a specific issue with sleep.

<?
set_time_limit(5);
while(true==true){
}
sleep(30);
echo 'done';
?>

按预期工作.

推荐答案

引用 手册:

set_time_limit() 函数和配置指令max_execution_time 只影响脚本的执行时间本身.在执行之外发生的活动上花费的任何时间使用system()进行系统调用、流操作、确定最大值时不包括数据库查询等脚本运行的时间.这在 Windows 上不是真的所测量的时间是真实的.

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(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

sleep 调用不会增加执行时间,因为它是一个 OS 进程,什么也没做.

The sleep call doesn't adds to the execution time because it's an OS process and nothing is being done.

这篇关于为什么 set_time_limit 没有按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆