您现在的位置是:热点 >>正文
注释掉 on('data') 请求为什么一直挂着?― 了解 Node.js Stream 的两种模式
热点436人已围观
简介...
这是来自「Nodejs技术栈」交流群一位读者朋友提的一个问题,“如果注释掉 req.on('data') 事件监听,end 事件就收不到了,进而永远也不会执行 res.end(),请求会被一直挂着,为什么?”。
如果你读到这里,也可以先思考下这个问题!
const http = require('http'); http.createServer((req, res) => { let dat注释a = ''; // req.on('data', chunk => { // data += chunk.toString(); // }); req.on('end', () => { res.end掉 (data); }); }).listen(3000);
Node.js 的可读流对象提供了两种模式:流动模式(flowing)、暂停模式(paused),如果你使用管道 pipe() 或异步迭代可能不会关注到这个问题,在它们的内部实现中已经处理好了,如果你是基于事件的 API 来处理流,可能会有这些疑问。
流动模式(flowing)
流动模式下数据自动从底层系统获取,并通过 EventEmitter 提供的事件接口,尽可能快的提供给应用程序。需要注意的是所有的可读流一开始都处于暂停模式,要切on换为流动模式,可通过以下几种方式实现:
一:注册 'data' 事件
为可读流对象注册一个 'data' 事件,传入事件处理函数,会把流切换为流动模式,在数据可用时会立即把数据块传送给注册的事件处理函数。
这也是上面的疑问,为什么注释掉 'data' 事件,请求就会一直被挂起。
req.on('data', chunk => { data += chunk.toString(); }); 二:stream.pipe() 方法
调('用 pipe() 方法将数据发送到可写流。
readable.pipe(writeable)
可读流的 pipe() 方法实现中也是注册了 'data' 事件,一边读取数据一边写入数据至可写流。可以参见笔者之前的这篇文章 Node.js Stream 模块 pipe 方法使用与实现原理分析。
Readable.prototype.pipe = function(dest, options) { const src = thidas; src.on('data', ondata); function ondata(chunk) { const ret = dest.write(chunk); if (ret === false) { &tanbsp; ... src.pause(); } } ... }; 三:stream.resume() 方法
stream.resume() 将处于暂停模式的可读流,恢复触发 'data' 事件,切换为流动模式。
对一开始的示例做一个改造,先调用 stream.resume(')) 用来耗尽流中的数据,但此时没有做任何的数据处理,之后会收到 end 事件。
const http = require('http'); http.createServer((req, res) => { req.resume(); req.on('end', () => { res 请.end('Ok!'); }); }).listen(3000); 四:异步迭代
无需注册事件监听函数,使用 for...await of 遍历可读流,写法上也很简单。下例,因为用到**顶级 await 特性,**需要在 ES Modules 规范中使用。
// app.mjs import { createServer as server } from 'http'; import&n求为bsp;{ on } from 'events'; const ee = on(server().listen(3000), 'request'); for await (const [{ url }, res] of ee) { res.end('OK!'); }
暂停模什么式
暂停模式也是流一开始时所处的模式,该模式下会触发 'readable' 事件,表示流中有可读取的数据,我们需要不断调用 read() 方法拉取数据,直到返回 null,表示缓冲区中的数据已被耗尽,在 read() 返回 null 后,会再次触发 'readable' 事件,表示仍有可读取的数据,如果此时停止 read() 方法调用,同样的请求也会被挂起。
stream.read(size) 方法从流缓冲区拉取数据,每次返回指定 size 大小的数据,如果不指定 size 则返回内部所有缓冲的数据。
const http = re一直quire('http'); http.createServer((req, res) => { let data = ''; let chunk; req.on('readable', () => { while 挂着(null !== (chunk = req.read())) { data += chunk.toString(); } }) req.on('end', () => { ?― res.end(data); }); }).listen(3000);
背压问题思考??
以流的形式从可读流拉取数据到可写流,通常**从磁盘读取数据的速度比磁盘写入的速度是快的,如果可写流来不及消费数据造成数据积压(专业术语会称呼这个问题为 “背压”)会怎么样?**也是来自「Nodejs技术栈」交流群读者朋友的疑问,可以思考下,答案可以写在评论区,感兴趣的关注下「Nodejs技术栈」下一次讲解。
总结
流刚开始处于暂停模式,所以注释掉 req.on 了('data') 事件监听,请求才会一直挂起。在基于流的方式读取文件时,之前通常使用注册 'data' 事件处理函数的方式从可读流中拉取数据,现在 Node.js 支持了异步迭代,更推荐你使用 for...await of 这种方式来读取数据,代码看起来也会更简洁,同步编码思维让人也能更好的理解。
Tags:
相关文章
标签: 辛帕智能 IPO 风电设备 工业智能
热点云梦级1月17日云梦级消息,据摩斯IPO报道,2025年12月31日,上海辛帕智能科技股份有限公司向北交所递交的IPO申请获得受理,公司计划募集资金3.3亿元,保荐机构为东方证券股份有限公司,律师事务...
【热点】
阅读更多标签: 新石器无人车 Neolix L4级自动驾驶 无人配送
热点云梦级2月25日云梦级消息,近日,新石器无人车(Neolix)宣布,其L4级无人车自动驾驶累计行驶里程突破1亿公里,成为全球无人配送行业首个达成亿公里运营里程的企业,这一里程碑标志着无人配送的规模化、...
【热点】
阅读更多标签: 企业微信 大圆 腾讯 张军
热点云梦级6月24日云梦级消息,腾讯集团公关总监张军23日发文称,企业微信即将推出名为“大圆”agent,目前已进入内测阶段。 张军表示,“大圆”更像是一个“长在企业微信里”的AI助理。该产品并不要求用户...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.hanbat.ac.kr/search/front/Search.jsp?qt=谷歌留痕外推【tg:seogoogle987】aKg
- https://www.tahiti-infos.com/search/谷歌留痕外推【tg:seogoogle987】,hi3
- https://www.selfsufficientish.com/main/?s=谷歌留痕外推【tg:seogoogle987】,deD
- https://www.xbiao.com/search/index?tpf&wd=谷歌留痕外推【tg:seogoogle987】,Y65
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=谷歌留痕外推【tg:seogoogle987】,Ici
- https://blvdseafood.com/?s=谷歌留痕外推【tg:seogoogle987】8cc
- https://www.therapeuticassociates.com/?cat=5&s=谷歌留痕外推【tg:seogoogle987】,HDF
- https://www.pixiv.net/tags/谷歌留痕外推【tg:seogoogle987】,swy
- https://www.youm7.com/Home/Search?allwords=谷歌留痕外推【tg:seogoogle987】,0K3
- https://www.livehindustan.com/tags/谷歌留痕外推【tg:seogoogle987】,Csv
- https://www.tycoonstory.com/?s=谷歌留痕外推【tg:seogoogle987】,kKf
- https://www.uctronics.com/catalogsearch/result/?q=谷歌留痕外推【tg:seogoogle987】,U2O
- https://www.qwant.com/?q=谷歌留痕外推【tg:seogoogle987】,APQ
- https://www.luxurysociety.com/en/search?q=谷歌留痕外推【tg:seogoogle987】,KAS
- https://www.abbreviations.com/谷歌留痕外推【tg:seogoogle987】k9l
- https://www.openspaceinstitute.org/search?q=谷歌留痕外推【tg:seogoogle987】,qjJ
- https://www.sonepar.ch/fr/search?q=谷歌留痕外推【tg:seogoogle987】,Ea5
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌留痕外推【tg:seogoogle987】2Vr
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌留痕外推【tg:seogoogle987】,f6q
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌留痕外推【tg:seogoogle987】,WZU
- https://www.kissthekangaroo.com.au/index.php?route=product/search&tag=谷歌留痕外推【tg:seogoogle987】,Oze
- https://www.ncetm.org.uk/search?action=CHECK&q=谷歌留痕外推【tg:seogoogle987】,R9l
- https://www.ussalabama.com/?action=CHECK&s=谷歌留痕外推【tg:seogoogle987】,T6Z
- https://www.terramania.nl/catalogsearch/result/index/?q=谷歌留痕外推【tg:seogoogle987】,0Ou
- https://www.lgnewsroom.com/search/谷歌留痕外推【tg:seogoogle987】,zQw
- https://wwwcp.umes.edu/search/谷歌留痕外推【tg:seogoogle987】,Pi1
- https://www.u-bourgogne.fr/?cat=117&s=谷歌留痕外推【tg:seogoogle987】,Lkx
- https://www.rhymezone.com/r/rhyme.cgi?typeofrhyme=oldrel&loc=nojs&Word=谷歌留痕外推【tg:seogoogle987】,UNh
- https://www.kuaikanmanhua.com/sou/谷歌留痕外推【tg:seogoogle987】,IJt
- https://www.supersauna.de/catalogsearch/result/?q=谷歌留痕外推【tg:seogoogle987】,SXy
- https://yscc1986.net/search/谷歌留痕外推【tg:seogoogle987】4Nh
- https://www.polliwogs.com/search/谷歌留痕外推【tg:seogoogle987】,CIn
- https://wydden.com/?ID=r8w9WMSKWkOcmiaFWaUv0dzMdqZWfKtF&s=谷歌留痕外推【tg:seogoogle987】,yxB
- https://www.revere.org/search?action=SEARCH&q=谷歌留痕外推【tg:seogoogle987】,RSY
- https://www.roblox.com/discover/?cat=107&Keyword=谷歌留痕外推【tg:seogoogle987】,7Lw
- https://www.teamlab.art/?submit=谷歌留痕外推【tg:seogoogle987】,eE7
- https://www.kaskus.co.id/search?q=谷歌留痕外推【tg:seogoogle987】,GdG
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌留痕外推【tg:seogoogle987】,ukR
- https://www.shopping.com/search.html?keyword=谷歌留痕外推【tg:seogoogle987】,MGZ
- https://dict.naver.com/dict.search?dicQuery&query=谷歌留痕外推【tg:seogoogle987】MSu
- https://www.remit.co.jp/id/?s=谷歌留痕外推【tg:seogoogle987】,T9K
- https://www.sic-info.org/en/?s=谷歌留痕外推【tg:seogoogle987】,UrG
- https://www.voices.com/talents/search?keywords=谷歌留痕外推【tg:seogoogle987】,PIU
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌留痕外推【tg:seogoogle987】2LB
- https://www.lauben.com/de/?s=谷歌留痕外推【tg:seogoogle987】,bZO
- https://www.theaerogroup.net/?cat=28&s=谷歌留痕外推【tg:seogoogle987】,h6C
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌留痕外推【tg:seogoogle987】,W11
- https://www.mintos.org/?s=谷歌留痕外推【tg:seogoogle987】,oy6
- https://www.normani.de/search?q=谷歌留痕外推【tg:seogoogle987】,L6R
- https://www.flickr.com/photos/tags/谷歌留痕外推【tg:seogoogle987】HzP