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

问题描述 投票:0回答:1

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

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

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

await pipeline(read, progress,write )

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

javascript node.js stream
1个回答
0
投票

嗯-实际上很简单-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

© www.soinside.com 2019 - 2024. All rights reserved.