NodeJs-事件循环是否只处理 I/O 请求?

NodeJs- does the Event Loop only handle I/O requests?

一般来说,事件循环只是针对 IO 的吗?什么是 IO 作业? 例如,假设一个请求进入 NodeJs,然后向 API 发出出站 http 请求以获取一些数据,同时不会阻止用户。

这是一个 IO 作业吗?NodeJ 将如何处理它?如果我想异步进行冗长的计算而不是 http 请求,然后 return 一个值给用户怎么办?尽管受到 CPU 约束,事件循环是否也处理了它?

In general is an event loop only for IO ?

我不会将计时器(setTimeoutsetInterval)和调度(setImmeadiateprocess.nextTick)算作 IO,但通常可以说事件在事件循环来自外部

and what exactly is an IO job?

这取决于您所谈论的上下文。每个程序从用户那里得到一定的输入,并产生一定的输出。例如,在终端中,输入是您的按键,输出是显示的内容。说到nodejs IO,一般指的是网络/文件操作,或者更笼统地说:代码不是用js写的。

For example let's say that a request comes into NodeJs which is then making an outbound http request to an API to get some data while not blocking the user in the meantime.

Is that an IO job and how would NodeJs handle it?

Nodejs 会产生一个发出请求的后台线程,同时主进程继续处理其他内容(继续处理事件队列中的其他事件)。然后,如果异步请求完成,后台进程将结果推送到事件队列,事件循环将从那里拉出它并执行回调等。

what if instead of the http request I wanted to asynchronously make a lengthy calculation and then return a value to the user?

你必须在 nodejs 中生成另一个线程,否则冗长的计算是同步的。

Is that handled by the event loop too despite being CPU bound?

一切都在某个时候落在事件循环上,并且一切都在 CPU ...

上执行