如何暂停和恢复NodeJS流管道?

编程入门 行业动态 更新时间:2024-10-06 17:16:43

如何暂停和恢复NodeJS<a href=https://www.elefans.com/category/jswz/34/1756248.html style=流管道?"/>

如何暂停和恢复NodeJS流管道?

我正在使用节点的stream.pipleline的承诺版本:

const pipeline = util.promisify(stream.pipeline);

//Create Read,Transform and Write streams....

await pipeline(read, progress,write )

我希望能够以编程方式暂停/恢复整个过程。如何在不中断其中任何一条流的情况下实现这一目标?

回答如下:

嗯-实际上很简单-pipeline在下面使用pipe,并在流之间转发pause / resume逻辑,所以:

如果您想暂停阅读:

const pipeline = util.promisify(stream.pipeline);

//Create Read,Transform and Write streams....

await pipeline(read, progress,write )

// under some circumstances:
read.pause();
// and then, when you're ready just
read.resume();

除了可写流,您可以在管道中的任何流上暂停。在到达progress时(基本上在缓冲区已满时),在read上调用暂停将导致暂停highWaterMark

更多推荐

如何暂停和恢复NodeJS流管道?

本文发布于:2024-05-07 21:41:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流管   NodeJS

发布评论

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

>www.elefans.com

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