使用流作为Node.js中异步队列的输入,如何确保只调用一次queue.drain

问题描述:

我将从流中读取一系列输入,并对每个输入执行HTTP GET请求.为了避免一次创建太多连接,我使用 async.queue 将这些输入排队

I will be reading a series of inputs from a stream, and perform a HTTP GET request per input. To avoid creating too many connections at a time, I am using async.queue to queue up these inputs.

读取所有输入(将end发送到流中)之后,我想收集以前的结果并生成概述.

After all inputs are read (end emitted to the stream), I would like to collect previous results and generate an overview.

我目前正在为此目的使用queue.drain.但是queue.drain在我的情况下可能会被多次调用,因为该过程可能在输入时被阻塞,并且在发生这种情况时队列将为空.

I am currently using queue.drain for this purpose. But queue.drain may be called multiple times in my case, since the process could be blocked on input and the queue will be empty when this happens.

那么,异步库中是否有任何东西可以确保在处理流时仅一次调用queue.drain?我不介意切换到另一个控制流,只要可以实现此功能即可.

So, is there anything in the async library that ensures queue.drain is called only once when dealing with streams? I don't mind switching to another control flow as long as this functionality could be realized.

在可读流上收到end事件时,只需添加drain处理程序即可.

Just add the drain handler when you receive the end event on your readable stream.

var s = new SomeReadableStream();
var q = queue(your_callback, 1);

s.on('end', function() {
  // Beware: if the queue is already empty, the drain callback will never be called,
  // we have to check this by ourselves
  if (q.running() === 0 && q.length() === 0) {
    drain_cb();
  }
  else {
    q.drain = drain_cb;
  }
});