Drupal6批处理API是如何工作的?

Drupal6批处理API是如何工作的?,drupal,drupal-batch,Drupal,Drupal Batch,我已经成功地使用了批处理API来处理通常会导致PHP超时或内存不足的错误,并且它工作得很好 我已经看过了一些代码,但我仍然不清楚幕后发生了什么 熟悉该流程的人能否描述其工作原理?来自: 每个批处理操作回调将反复迭代,直到 $context['finished']设置为1每次通过后,batch.inc将 检查它的计时器,看是否是新http请求的时间, i、 e.自上次请求后超过1分钟。 整个批处理过程非常快,可能只需要一个 http请求,即使它多次迭代回调, 而较慢的进程可能会在每个 回调的迭代

我已经成功地使用了批处理API来处理通常会导致PHP超时或内存不足的错误,并且它工作得很好

我已经看过了一些代码,但我仍然不清楚幕后发生了什么

熟悉该流程的人能否描述其工作原理?

来自:

每个批处理操作回调将反复迭代,直到 $context['finished']设置为1每次通过后,batch.inc将 检查它的计时器,看是否是新http请求的时间, i、 e.自上次请求后超过1分钟。

整个批处理过程非常快,可能只需要一个 http请求,即使它多次迭代回调, 而较慢的进程可能会在每个 回调的迭代

这意味着您应该在每次迭代中设置处理 只有在没有php超时的情况下才能做到,然后让batch.inc 决定是否需要发出新的http请求

换句话说:你必须将你的一批任务分成块(或单个任务),这样就不会超时。如果Drupal看到PHP超时即将到来,它将结束当前调用并打开一个新的HTTP请求

我已经看过了一些代码,但我仍然不清楚幕后发生了什么

熟悉这一过程的人能描述一下它是如何工作的吗

为了避免PHP超时,浏览器会定期通过AJAX ping导致执行批处理操作的URL($id)。
请参见“批处理”路径的菜单回调函数,该函数由调用

在中,您将注意到以下代码

  $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op)));
  drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL=' . $url . '">');
  $output = theme('progress_bar', $percentage, $message);
  return $output;
启用JavaScript时,执行所有工作的代码都在文件中

批处理URL的轮询从
progress.startMonitoring(uri+'&op=do',10)
开始。batch.js文件依赖于文件中定义的
Drupal.progressBar
中公开的功能

Drupal7中使用了类似的代码,它使用的、和文件的版本略有不同

提供批处理页面时,Drupal设置为关机回调;当PHP因超时而关闭时,函数将更新数据库中的批处理数组

// Drupal 6.
function _batch_shutdown() {
  if ($batch = batch_get()) {
    db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
  }
}

谢谢你的回答。这些优秀的文档是我用来让事情顺利进行的。我想知道的是它是如何工作的,而不是如何使用它。我强调了回答你问题的部分。还是我误解了你的问题?
  // Merge required query parameters for batch processing into those provided by
  // batch_set() or hook_batch_alter().
  $batch['url_options']['query']['id'] = $batch['id'];
  $batch['url_options']['query']['op'] = $new_op;

  $url = url($batch['url'], $batch['url_options']);
  $element = array(
    '#tag' => 'meta', 
    '#attributes' => array(
      'http-equiv' => 'Refresh', 
      'content' => '0; URL=' . $url,
    ),
  );
  drupal_add_html_head($element, 'batch_progress_meta_refresh');

  return theme('progress_bar', array('percent' => $percentage, 'message' => $message));
/**
 * Attaches the batch behavior to progress bars.
 */
Drupal.behaviors.batch = function (context) {
  // This behavior attaches by ID, so is only valid once on a page.
  if ($('#progress.batch-processed').size()) {
    return;
  }
  $('#progress', context).addClass('batch-processed').each(function () {
    var holder = this;
    var uri = Drupal.settings.batch.uri;
    var initMessage = Drupal.settings.batch.initMessage;
    var errorMessage = Drupal.settings.batch.errorMessage;

    // Success: redirect to the summary.
    var updateCallback = function (progress, status, pb) {
      if (progress == 100) {
        pb.stopMonitoring();
        window.location = uri+'&op=finished';
      }
    };

    var errorCallback = function (pb) {
      var div = document.createElement('p');
      div.className = 'error';
      $(div).html(errorMessage);
      $(holder).prepend(div);
      $('#wait').hide();
    };

    var progress = new Drupal.progressBar('updateprogress', updateCallback, "POST", errorCallback);
    progress.setProgress(-1, initMessage);
    $(holder).append(progress.element);
    progress.startMonitoring(uri+'&op=do', 10);
  });
};
(function ($) {

/**
 * Attaches the batch behavior to progress bars.
 */
Drupal.behaviors.batch = {
  attach: function (context, settings) {
    $('#progress', context).once('batch', function () {
      var holder = $(this);

      // Success: redirect to the summary.
      var updateCallback = function (progress, status, pb) {
        if (progress == 100) {
          pb.stopMonitoring();
          window.location = settings.batch.uri + '&op=finished';
        }
      };

      var errorCallback = function (pb) {
        holder.prepend($('<p class="error"></p>').html(settings.batch.errorMessage));
        $('#wait').hide();
      };

      var progress = new Drupal.progressBar('updateprogress', updateCallback, 'POST', errorCallback);
      progress.setProgress(-1, settings.batch.initMessage);
      holder.append(progress.element);
      progress.startMonitoring(settings.batch.uri + '&op=do', 10);
    });
  }
};

})(jQuery);
  // The WAI-ARIA setting aria-live="polite" will announce changes after users
  // have completed their current activity and not interrupt the screen reader.
  this.element = $('<div class="progress" aria-live="polite"></div>').attr('id', id);
  this.element.html('<div class="bar"><div class="filled"></div></div>' +
                    '<div class="percentage"></div>' +
                    '<div class="message">&nbsp;</div>');
// Drupal 6.
function _batch_shutdown() {
  if ($batch = batch_get()) {
    db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
  }
}
// Drupal 7.
function _batch_shutdown() {
  if ($batch = batch_get()) {
    db_update('batch')
      ->fields(array('batch' => serialize($batch)))
      ->condition('bid', $batch['id'])
      ->execute();
  }
}