【问题标题】:How to pause and resume a NodeJS pipeline of streams?如何暂停和恢复 NodeJS 流管道?
【发布时间】:2020-05-14 21:37:14
【问题描述】:

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

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

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

await pipeline(read, progress,write )

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

【问题讨论】:

    标签: javascript node.js stream


    【解决方案1】:

    嗯 - 实际上很简单 - 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 调用 pause 将导致在到达 highWaterMark 时暂停 read(基本上是在缓冲区已满时)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多