node.js 如何实现非阻塞 I/O? [英] How does node.js implement non-blocking I/O?

查看:72
本文介绍了node.js 如何实现非阻塞 I/O?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

here 我发现 node.js 实现了非阻塞 i/o模型.但我不明白怎么做.

From here i have found that node.js implements non-blocking i/o model. But i don't understand how.

因为 javascript 是单线程的.单个线程如何进行 I/O 操作并同时执行后续进程.

As javascript is single threaded. How can a single thread do i/o operations and simultaneously executing the further process.

推荐答案

sleep 等操作确实会阻塞线程.但 I/O 事件确实可以是异步的.

It is true that operations such as sleep will be blocking the thread. But I/O events can indeed be asynchronous.

Node.js 为此使用事件循环.事件循环是处理和处理外部事件并将它们转换为回调调用的实体"

Node.js uses an event loop for this. An event loop is "an entity that handles and processes external events and converts them into callback invocations"

每当需要数据时,nodejs 都会注册一个回调并将操作发送到此事件循环.只要数据可用,就会调用回调.

Whenever data is needed nodejs registers a callback and sends the operation to this event loop. Whenever the data is available the callback is called.

http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ 了解更多信息

这篇关于node.js 如何实现非阻塞 I/O?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆