標籤:
PHP設定檔的參數max_execution_time表示指令碼執行逾時時間
max_execution_time=0表示不限制
max_execution_time=2表示執行兩秒後終止,同時報錯Fatal error: Maximum execution time of 2 seconds exceeded
但是,sleep(10)函數是不起作用的。
例如,php.ini中的max_execution_time=2
執行代碼 <?php sleep(10);echo ‘ok‘; ?> 是不會逾時的(前提是web伺服器不逾時)
執行代碼 <?php echo 1;for($i=0;$i<100000000000;$i++){ for($i=0;$i<100000000000;$i++){}} echo 2;?> 會逾時,輸出1,且報錯
註:set_time_limit()函數是可以覆蓋max_execution_time參數的,可以比max_execution_time參數大,也可以為0表示不限制,當然
當此函數被調用時,set_time_limit()會從零開始重新啟動逾時計數器。換句話說,如果逾時預設是30秒,在指令碼運行了了25秒時調用 set_time_limit(20),那麼,指令碼在逾時之前可運行總時間為45秒
還有一種情況
如果PHP工作在php-fpm模式,例如web伺服器是nginx
這時候的逾時還取決於php-fpm.conf中的配置request_terminate_timeout
這個參數是這麼解釋的:the timeout for serving a single request after which the worker process will be killed. This option should be used when the ‘max_execution_time‘ ini option does not stop script execution for some reason.
大概的意思是如果max_execution_time這個參數沒有限制指令碼的執行,就取決於這個參數的逾時,例如,max_execution_time=0;或者max_execution_time大於request_terminate_timeout的值,或者程式中有sleep(),最終的逾時都取決於request_terminate_timeout參數;
當然如果程式是因為request_terminate_timeout逾時是不會有內容輸出的,會報502錯誤,502 Bad Gateway/Nginx 1.7
綜上,總結下列對照表,如果有不對的地方,歡迎大家指正
| |
非fpm
max_execution_time=4 |
fpm
request_terminate_timeout=8
max_execution_time=4 |
fpm
request_terminate_timeout=4
max_execution_time=8 |
fpm
request_terminate_timeout=4
max_execution_time=0 |
|
<?php
echo 1;sleep(10);echo 2;
?> |
輸出:12 |
輸出:502 Bad Gateway |
輸出:502 Bad Gateway |
輸出:502 Bad Gateway |
|
<?php
echo 1;$i=0;
while($i++<1000000000)
{//假設會執行很久}
echo 2;
?> |
輸出:1
Fatal error: Maximum execution
time of 4 seconds exceeded |
輸出:1
Fatal error: Maximum execution
time of 4 seconds exceeded |
輸出:502 Bad Gateway |
輸出:502 Bad Gateway |
關於PHP執行逾時的問題