php set_time_limit() 和 sleep()

我们知道,在php中可以 用函数 set_time_limit() 设定脚本执行时间。
但如下程序会造成意想不到的结果,

set_time_limit(5);
for($i=0;$i<100;$i++)                                                           
{
      echo "i=$i\n";
      sleep(1);
}

执行后,会发现程序并没有在5秒后终止,而是不断的输出…..查询手册后,发现下面这句:
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(), 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. 

也就是说 set_time_limit()函数 仅计算脚本自己执行的时间,调用system(),io操作,数据库查询,sleep() 函数不计算在脚本执行时间之内,这就是为什么上面的脚本不会超时的原因 :) .
当然,我们还需要注意的另一个重点就是
This function has no effect when PHP is running in safe mode.

安全模式下 set_time_limit()是不起作用的 :0

Posted in php分享 |

2 Comments.

  1. dennis 说:

    继续努力,总结的很好

Leave a Reply

[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Get Adobe Flash playerPlugin by wpburn.com wordpress themes