javascript - 如何暂停和恢复 NodeJS 流管道?
问题描述
我正在使用 node 的 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();
您可以暂停管道中的任何流,但可写流除外。调用 pause onprogress
将导致在到达时read
立即暂停highWaterMark
(基本上是在缓冲区已满时)。
推荐阅读
- amazon-web-services - AWSIoTPythonSDK publishTimeoutException
- python - 在条件下在df中添加和填充行
- mysql - Mysql 在 FULLTEXT 索引具有相同数据的两台服务器之间显示不同的结果
- typescript - React-Admin 从 aws amplify schema.graphql 中的“data-generator-retail”转换 generateData
- python-3.x - 在 Python 中使谷歌驱动器访问令牌动态化
- java - android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=PendingIntent{ }
- python - 如何关闭文件以便可以使用 os.remove() 删除它?获取 WinError32
- c - valgrind 在分配的内存中写入大小为 8 的无效
- node.js - 未捕获的集成错误:无效的 stripe.redirectToCheckout 参数:items.0.price 不是可接受的参数
- javascript - Fullcalender V5 RefetchEvents 问题