经过一定量的查询/解析时间后停止脚本(无致命错误) [英] Stop a script (without fatal error) after a certain amount of query/parse time
问题描述
我有一个脚本,该脚本从外部数据库提取并将数据存储在本地数据库中.我在共享服务器环境上进行了设置,因此我无法让脚本运行超过两分钟,但是运行大约需要5-10分钟才能完成.
I have a script that pulls from an external database and stores the data in a local database. I'm setup on a shared-server environment so I'm not able to let the script run for longer than two minutes, yet it would take around 5-10 minutes to run to completion.
我可以让脚本在一个半分钟后停止它的foreach循环,以便我可以重定向到相同的脚本,但是使用不同的数据库偏移量来提取它的剩余位置吗?
Can I have the script stop its foreach loop after one and a half minutes, so I can redirect to the same script but with a different database offset to pick up where it left?
我可以使用GET查询字符串完成其中的最后一部分,但不确定如何计时.
I can do the last part of that using a GET query string, but I'm unsure how to time it.
推荐答案
最简单的方法是在脚本开始处设置time()并检查foreach循环中的差异.
The easiest way would be to set a time() at the start of your script and check the difference in your foreach loop.
$start = time(); // Returns time in seconds
foreach($bigdata as $row) {
if(time()-$start > 100) { // Stop 20 seconds before 120 sec limit
// Some code for exiting the loop...
break;
}
}
这篇关于经过一定量的查询/解析时间后停止脚本(无致命错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!