javascript - 在Node.js官网API中,关于readable._read(size)的疑问?
大家讲道理
大家讲道理 2017-04-10 16:58:27
[JavaScript讨论组]

在学习Node.js的时候,查看官网文档,关于readable._read(size)这个API,官网中有如下的这段话:
When _read is called, if data is available from the resource, _read should start pushing that data into the read queue by calling this.push(dataChunk). _read should continue reading from the resource and pushing data until push returns false, at which point it should stop reading from the resource. Only when _read is called again after it has stopped should it start reading more data from the resource and pushing that data onto the queue.

Note: once the _read() method is called, it will not be called again until the push method is called.

问题如下:

  1. 为什么_read会触发push操作?

  2. _read为什么会设计成关联push的操作?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
高洛峰

1)_read方法作为底层借口由你自己实现readable接口的时候实现,不能直接调用
2)_read方法从底层数据流中读取数据内容,然后通过push方法放入用户的数据消费队列中,直到用户数据消费队列满。此时数据的读取动作就为停止。
3)当用户消费队列中的数据在用户通过监听data事件获取到后,_read方法会被再次调用,填充用户数据消费队列
4)这个过程一直持续到读到数据末尾位置

_read方法是需要在实现自己的readable流对象的时候实现的,是底层系统调用触发的。你不好直接调用~~~

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号