掌握Node JS替代多线程 [英] Grasping the Node JS alternative to multithreading

查看:73
本文介绍了掌握Node JS替代多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确,Node JS不会阻塞...因此,它无需等待数据库或其他进程的响应,而是继续进行其他操作并稍后再检查.

它也是单线程的.

所有这一切都意味着给定的Node JS进程可以完全有效地利用单个CPU内核,但不会使用计算机上的任何其他内核,因为它一次不会使用多个内核. /p>

这当然意味着,其他CPU仍可以被其他进程用于SQL数据库或其他有意分离的CPU重子例程之类的东西,只要它们是单独的进程即可.

此外,如果Node JS进程具有无限循环或长时间运行的功能,则在停止无限循环或长时间运行的功能(或终止整个过程)之前,该进程将不再有用.

这可以吗?我的理解正确吗?

解决方案

是的,非常正确. node.js服务器具有内部线程池,因此它可以执行阻止操作,并在事情完成时通过回调或事件通知主线程.

因此,我想它会在线程池中有限地使用另一个内核,例如,如果您执行非阻塞文件系统读取,则很可能是通过告诉线程池中的线程执行读取和设置来实现的完成后的回调,这意味着在主node.js程序正在执行其他操作时,读取可能在其他线程/内核上进行.

但是从node.js的角度来看,它完全是单线程的,不会直接使用多个内核.

If I understand correctly Node JS is non blocking...so instead of waiting for a response from a database or other process it moved on to something else and checks back later.

Also it is single threaded.

So does all this mean that a given Node JS process can fully and efficiently utilize a single CPU core but it will not use any other core on the machine, as in, it will never use more than one at a time.

This of course means that the other CPUs can still be used by other processes for things like SQL database or other intentionally separated CPU heavy subroutines as long as they are a separate process.

Also in the event that the Node JS process has an endless loop or long running function, that process is no longer useful in any way until the endless loop or long running function is stopped (or whole process killed).

Is all this right? Am I correct in my understanding?

解决方案

Pretty much correct, yes. The node.js server has an internal thread pool so it can perform blocking operations and notify the main thread with a callback or event when things complete.

So I imagine that it will make limited use of another core for the thread pool, for example if you do a non-blocking file system read this is likely implemented by telling a thread from the thread pool to perform a read and set a callback when it's done which means that the read could be happening on a different thread/core while the main node.js program is doing something else.

But from a node.js point of view, it's entirely single threaded and won't directly use more than one core.

这篇关于掌握Node JS替代多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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