带有进度条和处理超时的PHP批处理类

时间:2014-05-17 20:35:10

标签: php apache batch-processing

我正在使用Soundcloud API来获取群组成员的信息。现在一组中有成千上万的成员。我需要在“批处理”中完成此操作,并且必须显示进度条。我怎样才能做到这一点?请问任何技术解决方案?

编辑:班级应该牢记“服务器负载”。我的意思是说,服务器不应该非常忙于处理这个并且CPU负载不应该变高。

1 个答案:

答案 0 :(得分:0)

对于进度条部分,我在评论中提到的问题似乎没有提供正确的答案。通过一些进一步的谷歌搜索和实验,我找到了这个解决方案,这似乎适用于Chrome和IE11。

<?php 
  set_time_limit(0); // Don't timeout.
  header( 'Content-type: text/html; charset=utf-8' ); 
?><!doctype html>
<html>
<body>
<progress id="progress" max="10" value="1">
</progress>
<div id="test">
Hello world
</div>

<?php
// Apparently this is needed to disable buffering.
ob_implicit_flush(true);
ob_end_flush();

$colors = array('red', 'green', 'blue', 'yellow', 'red', 'green', 'black', 'white', 'blue', 'green', 'pink');
for ($i = 1; $i <= 10; ++$i)
{
  // Outputting a large chunk of data is also mentioned as a work-around, but for me it didn't work.
  // echo str_repeat(' ', 10000);

  // Instead of sleeping, this is where your actual processing would go.
  sleep(1);

  // Echo a small Javascript to update the status.
  ?>
    <script>
    document.getElementById('progress').value = <?=$i?>;
    document.getElementById('test').style.backgroundColor = '<?=$colors[$i]?>';
    document.getElementById('test').innerText = '<?=$i*10?>% done';
    </script>
  <?php
  // The script is flushed right away, and the browser should execute it right away.
  flush();
}