php - 带偏移量的递归目录迭代器

标签 php file-io recursion

是否可以从某个点开始循环?

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, $flags));

$startTime = microtime(true); 
foreach($iterator as $pathName => $file){

  // file processing here

  // after 5 seconds stop and continue in the next request
  $elapsedSecs = (microtime(true) - $startTime);
  if($elapsedSecs > 5)
     break;
}

但是我如何在下一个请求中从我的断点恢复呢?

最佳答案

a) 从 foreach 中提取时间计算。你有一个开始时间,你想要 5 秒的运行时间,所以你可以预先计算结束时间 (startime+5s)。在 foreach 内部,简单地比较时间是否大于或等于结束时间,然后中断。

b) 问:是否可以从某个点开始循环?如何在下一个请求中从断点恢复?

我想到了两种方法。

您可以存储最后一个处理点和迭代器,并在最后一个点 + 1 处恢复。 您将保存迭代的最后位置并在下一个请求时快进到它,方法是调用 iterator->next() 直到到达下一个要处理的项目,即 $lastPosition+1。 我们必须存储迭代器和 lastPosition 并在下一个请求时获取它们,直到 lastPosition 等于迭代器中元素的总数。

或者,您可以在第一次运行时将迭代器转换为数组:$array = iterator_to_array($iterator); 然后使用 reduce 数组方法。 (也许其他人知道如何减少迭代器对象。) 使用这种方法,您将只存储数据,这会逐个减少请求直到 0。

代码未经测试。这只是一个快速草稿。

$starttime = time();
$endtime = $starttime + (5 * 60); // 5sec
$totalElements = count($array);

for($i = 0; $i <= $totalElements; $i++) 
{
    if(time() >= $endtime) {
        break;
    }

    doStuffWith($array[$i]);
}

echo 'Processed ' . $i . ' elements in 5 seconds';

// exit condition is "totalElements to process = 0"
// greater 1 means there is more work to do
if( ($totalElements - $i) >= 1) {

    // chop off all the processed items from the inital array
    // and build the array for the next processing request
    $reduced_array = array_slice(array, $i);

    // save the reduced array to cache, session, disk    
    store($reduced_array);
} else {
    echo 'Done.';
}

// on the next request, load the array and resume the steps above...

总而言之,这是批处理,可能由工作人员/工作队列更有效地完成,例如:

关于php - 带偏移量的递归目录迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27547581/

相关文章:

php - 准备好的语句未获取所有结果

php - 带有 ISP Config 3 的 Centos 6.4 中 phpmyadmin 的主 php.ini 文件

php - 使用 FreeTDS 库通过 Zend Framework 连接 MSSQL Server

java - 同步线程等待多个线程

无法通过 C 中的 fwrite 和 fread 复制文本文件

haskell - 无法在我的 Haskell 程序中找到错误(来自 Project Euler 的难题 #2)

php - 尝试检查 $_GET 是否有空参数

javascript - 使用 JavaScript 在浏览器中打开文件时写入文件

c++ - 在 C++ 中使用递归反转数组

javascript - 为什么函数返回未定义?