Node.js 事件循环 [英] Node.js Event loop

查看:29
本文介绍了Node.js 事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js I/O 事件循环是单线程还是多线程?

如果我有多个 I/O 进程,node 会将它们放在一个外部事件循环中.它们是按顺序处理的(最快的先)还是处理事件循环以同时处理它们(......以及在哪些限制中)?

解决方案

事件循环

Node.js 事件循环在单线程下运行,这意味着您编写的应用程序代码在单线程上进行评估.Nodejs 本身通过 libuv 使用了很多底层线程,但是在编写 nodejs 代码时你永远不必处理这些.

每个涉及 I/O 调用的调用都需要您注册一个回调.此调用也会立即返回,这允许您在不使用应用程序代码中的线程的情况下并行执行多个 IO 操作.一旦 I/O 操作完成,它的回调将被推送到事件循环中.它将在执行之前在事件循环上推送的所有其他回调后立即执行.

有几种方法可以对如何将回调添加到事件循环中进行基本操作.通常您不需要这些,但它们有时会很有用.

在任何时候都不会有两条真正的并行执行路径,因此所有操作本质上都是线程安全的.通常会有几个异步并发执行路径由事件循环管理.

阅读有关事件循环的更多信息

限制

由于事件循环,节点不必为每个传入的 tcp 连接启动一个新线程.这允许节点服务数十万个请求 同时,只要您不计算每个请求的前 1000 个素数.

这也意味着不要进行 CPU 密集型操作很重要,因为这些操作会锁定事件循环并阻止其他异步执行路径继续.不使用所有 I/O 方法的 sync 变体也很重要,因为它们也会锁定事件循环.

如果你想做 CPU 繁重的事情,你应该把它委托给一个不同的进程,它可以更有效地执行 CPU 绑定操作,或者你可以把它写成 节点本机添加.

阅读有关用例的更多信息

控制流程

为了管理编写许多回调,您可能需要使用控制流库.我相信这是目前最流行的基于回调的库:

我使用过回调,它们几乎把我逼疯了,我使用 Promise 的体验要好得多,bluebird 是一个非常流行且快速的 Promise 库:

我发现这是节点社区中一个非常敏感的话题(回调与承诺),因此无论如何,请使用您认为最适合您个人的方式.一个好的控制流库还应该为您提供异步堆栈跟踪,这对于调试非常重要.

当事件循环中的最后一个回调完成其执行路径并且不注册任何其他回调时,Node.js 进程将完成.

这不是一个完整的解释,我建议您查看以下线程,它是最新的:

如何开始使用 Node.js

Is the Node.js I/O event loop single- or multithreaded?

If I have several I/O processes, node puts them in an external event loop. Are they processed in a sequence (fastest first) or handles the event loop to process them concurrently (...and in which limitations)?

解决方案

Event Loop

The Node.js event loop runs under a single thread, this means the application code you write is evaluated on a single thread. Nodejs itself uses many threads underneath through libuv, but you never have to deal with with those when writing nodejs code.

Every call that involves I/O call requires you to register a callback. This call also returns immediately, this allows you to do multiple IO operations in parallel without using threads in your application code. As soon as an I/O operation is completed it's callback will be pushed on the event loop. It will be executed as soon as all the other callbacks that where pushed on the event loop before it are executed.

There are a few methods to do basic manipulation of how callbacks are added to the event loop. Usually you shouldn't need these, but every now and then they can be useful.

At no point will there ever be two true parallel paths of execution, so all operations are inherently thread safe. There usually will be several asynchronous concurrent paths of execution that are being managed by the event loop.

Read More about the event loop

Limitations

Because of the event loop, node doesn't have to start a new thread for every incoming tcp connection. This allows node to service hundreds of thousands of requests concurrently , as long as you aren't calculating the first 1000 prime numbers for each request.

This also means it's important to not do CPU intensive operations, as these will keep a lock on the event loop and prevent other asynchronous paths of execution from continuing. It's also important to not use the sync variant of all the I/O methods, as these will keep a lock on the event loop as well.

If you want to do CPU heavy things you should ether delegate it to a different process that can execute the CPU bound operation more efficiently or you could write it as a node native add on.

Read more about use cases

Control Flow

In order to manage writing many callbacks you will probably want to use a control flow library. I believe this is currently the most popular callback based library:

I've used callbacks and they pretty much drove me crazy, I've had much better experience using Promises, bluebird is a very popular and fast promise library:

I've found this to be a pretty sensitive topic in the node community (callbacks vs promises), so by all means, use what you feel will work best for you personally. A good control flow library should also give you async stack traces, this is really important for debugging.

The Node.js process will finish when the last callback in the event loop finishes it's path of execution and doesn't register any other callbacks.

This is not a complete explanation, I advice you to check out the following thread, it's pretty up to date:

How do I get started with Node.js

这篇关于Node.js 事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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