猿问

如何运行长循环需要超过允许的最大执行时间

我有一个foreach循环,但需要很长时间才能到达终点,并且该时间超过max_execution_time了我的共享托管服务器允许的最大执行时间。


我也不能改变max_execution_time价值,因为我知道这样的解决方案


set_time_limit(0); // if `safe_mode` is off

// or

ini_set('max_execution_time', '300'); //300 seconds

一切都不起作用,我不允许这样做。


所以我想知道是否有任何方法喜欢sleep或停止循环后一段时间sleep再继续!任何想法这样做。


$arr = range(0, 50000000);


foreach ($arr as $number){


// do something using $number


}


绝地无双
浏览 203回答 2
2回答

慕莱坞森

只需使用 cookie 存储每个循环块的计数器,然后重定向到同一页面使用 cookie 拆分循环<?php&nbsp; &nbsp; $start = microtime(true);&nbsp; &nbsp; if (!isset($_COOKIE['count']) && !is_integer($_COOKIE['count'])){&nbsp; &nbsp; setcookie('count', 0);&nbsp; &nbsp; header("Location: http://my.loc/Myclass.php");&nbsp; &nbsp; exit;}&nbsp; &nbsp; $count = $_COOKIE['count'];&nbsp; &nbsp; $arr = range($count, 5000000);&nbsp; &nbsp; $handle = fopen('text.txt', 'a+');&nbsp; &nbsp; foreach ($arr as $item){&nbsp; &nbsp; &nbsp; &nbsp; fwrite($handle, $item . ',');&nbsp; &nbsp; &nbsp; &nbsp;if ($item%1000000 == 999999 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setcookie('count', ++$item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header("Location: http://my.loc/Myclass.php");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;&nbsp; &nbsp; &nbsp; &nbsp;}elseif($item == 5000000){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setcookie('count', 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$end = microtime(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setcookie('time1', $end - $start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }另一种不使用cookies来拆分循环的解决方案<?php&nbsp; &nbsp; $start = microtime(true);&nbsp; &nbsp; if (!isset($_GET['count'])){&nbsp; &nbsp; &nbsp; &nbsp; $count = 1;&nbsp; &nbsp; &nbsp; &nbsp; header("Location: http://my.loc/Myclass1.php?count=$count");&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }&nbsp; &nbsp; $count = $_GET['count'];&nbsp; &nbsp; $arr = range($count, 500000);&nbsp; &nbsp; $handle = fopen('text.txt', 'a+');&nbsp; &nbsp; foreach ($arr as $item){&nbsp; &nbsp; &nbsp; &nbsp; fwrite($handle, $item . ',');&nbsp; &nbsp; &nbsp; &nbsp;if ($item%100000 == 99999){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;++$item;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header("Location: http://my.loc/Myclass1.php?count=$item");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;&nbsp; &nbsp; &nbsp; &nbsp;}elseif($item == 500000){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$end = microtime(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setcookie('time1', $end - $start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }

慕姐4208626

我知道您不允许对ini_set.&nbsp;因此,如果您更改命令行的设置,它不会影响 Web 请求的最大执行时间。当您说这ini_set不起作用时,我认为您没有意识到这不是特定问题的具体问题,因此我必须假设您的情况下不起作用意味着什么。我认为这意味着即使您设置了更大的限制,循环仍然会超时。但是,如果您运行ini_set('max_execution_time',&nbsp;0);然后脚本将运行到最后。如果它是由操作系统发出的,例如作为 cron 作业而不是来自 Web 请求,那么 Web 请求的上限执行时间将保持不变,即使您运行ini_set.尽管这对于解决方案来说应该足够了,但值得一提的是,如果由于某些奇怪的原因您ini_set甚至不允许为 cli php.ini调用,那么您将不得不在超时之前向同一页面发送请求,传递您的索引,因此下一个请求将知道从哪里继续。不要把它分成几个文件,这是一种反模式。无论您选择哪种解决方案,请尝试优化您的代码。如果代码没有浪费时间,那么执行时间可能不会有任何问题。
随时随地看视频慕课网APP
我要回答