duanba7653 2012-04-20 03:14
浏览 37

如何将一个非常长的内存密集型循环拆分为更小的块

I have quite a long, memory intensive loop. I can't run it in one go because my server places a time limit for execution and or I run out of memory.

I want to split up this loop into smaller chunks.

I had an idea to split the loop into smaller chunks and then set a location header to reload the script with new starting conditions.

MY OLD SCRIPT (Pseudocode. I'm aware of the shortcomings below)

for($i=0;$i<1000;$i++)
{
  //FUNCTION
}

MY NEW SCRIPT

$start=$_GET['start'];
$end=$start+10;

for($i=$start;$i<$end;$i++;)
{
   //FUNCTION
}

header("Location:script.php?start=$end");

However, my new script runs successfully for a few iterations and then I get a server error "Too many redirects"

Is there a way around this? Can someone suggest a better strategy?

I'm on a shared server so I can't increase memory allocation or script execution time.

I'd like a PHP solution.

Thanks.

  • 写回答

6条回答 默认 最新

  • dongxian6715 2012-04-20 03:18
    关注

    First off, without knowing what your doing inside the loop, it's hard to tell you the best approach to actually solving your issue. However, if you want to execute something that takes a really long time, my suggestion would be to set up a cron job and let it nail out little portions at a time. The script would log where it stops and the next time it starts up, it could read the log for where to start.


    Edit: If you are dead set against cron, and you aren't too concerned about user experience, you could do this:

    Let the page load similar to the cron job above. Except after so many seconds or iterations, stop the script. Display a refresh meta tag or javascript refresh. Do this until the task is done.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP