使用stream作为Node.js中的asynchronous队列的input,如何确保queue.drain只被调用一次
我将从stream中读取一系列input,并对每个input执行HTTP GET请求。 为了避免一次创build太多的连接,我使用async.queue来排队这些input。
在所有的input被读取后( end
发送到stream),我想收集以前的结果,并产生一个概述。
我目前正在使用queue.drain
来达到这个目的。 但在我的情况下,可能会多次调用queue.drain
,因为进程在input时可能被阻塞,并且在发生这种情况时队列将为空。
那么,asynchronous库中有没有什么可以确保queue.drain
在处理stream时只调用一次呢? 只要这个function可以实现,我不介意切换到另一个控制stream程。
当您在可读stream上收到end
事件时,只需添加drain
处理程序即可。
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; } });
- 如何将此代码从同步传输到asynchronous(nodejs)?
- 我应该如何批量上传到s3,并通过最终callback从nodeJS webserver插入到MongoDB?
- pipe道过滤和lodash处理
- Node.js Web服务器fs.createReadStream vs fs.readFile?
- Node.jsasynchronous数组迭代
- forEach Loop中的asynchronousfindOne()操作
- 如何在启动funcC()之前执行未定义数目的funcA()和funcB()?
- 在返回之前等待asynchronous完成
- 在NodeJs 8. *如何在http.get上应用Async / Await?