node.js如何通过背压处理快速生产者和缓慢消耗者

编程入门 行业动态 更新时间:2024-10-10 12:21:50
本文介绍了node.js如何通过背压处理快速生产者和缓慢消耗者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是node.js的新手,不了解有关流的文档.希望得到一些提示.

I'm very novice in node.js and don't understand the documentation about streams. Hoping to get some tips.

我正在读取一个非常大的文件行,然后针对每一行调用一个异步网络api.

I'm reading a very large file line, and then for each line I'm calling an async network api.

很明显,本地文件的读取速度比异步调用完成的速度快得多:

Obviously the local file is read much faster than the async calls are completed:

var lineReader = require('readline').createInterface({ input: require('fs').createReadStream(program.input) }); lineReader.on('line', function (line) { client.execute(query, [line], function(err, result) { // needs to pressure the line reader here var myJSON = JSON.stringify(result); console.log("line=%s json=%s",myJSON); }); });

在执行"方法中增加背压的方法是什么?

What is the way to add back pressure in the "execute" method?

推荐答案

解决方案是将异步行为包装在流编写器中,并从编写器内部限制异步阅读器:

The solution is to wrap the async behavior in a stream writer and throttle the async reader from within the writer:

val count = 0; var writable = new stream.Writable({ write: function (line, encoding, next) { count++; if (count < concurrent) { next(); } asyncFunctionToCall(...) { // completion callback // reduce the count and release back pressure count--; next(); ... } }); var stream = fs.createReadStream(program.input, {encoding: 'utf8'}); stream = byline.createStream(stream); stream.pipe(writable);

更多推荐

node.js如何通过背压处理快速生产者和缓慢消耗者

本文发布于:2023-11-25 16:53:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生产者   缓慢   消耗   快速   node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!