简体   繁体   English

单线程事件循环与Node.JS中的多线程非阻塞工作器

[英]Single Threaded Event Loop vs Multi Threaded Non Blocking Worker in Node.JS

Node.JS biggest advantage is it's non blocking nature. Node.JS最大的优点是它具有非阻塞性。 It's single threaded, so it doesn't need to spawn a new thread for each new incoming connection. 它是单线程的,因此不需要为每个新的传入连接生成一个新线程。

Behind the event-loop (which is in fact single threaded), there is a "Non blocking Worker". 在事件循环(实际上是单线程)后面,有一个“非阻塞工作者”。 This thing is not single threaded anymore, so (as far as I understood) it can spawn a new thread for each task. 这个东西不再是单线程了,所以(据我所知)它可以为每个任务生成一个新线程。

Maybe I misunderstood something, but where exactly is the advantage. 也许我误解了一些东西,但究竟哪里有优势。 If there are to many tasks to handle, wouldn't be the Non Blocking Working turn into a Blocking Worker? 如果要处理的任务很多,那么非阻塞工作会不会变成阻塞工作者?

Thanks Christian 谢谢Christian

You need to read about libuv , the "magic" behind node's non-blocking I/O. 您需要了解libuv ,它是节点的非阻塞I / O背后的“神奇”。

The important thing to take away from the libuv book is that libuv uses the host OS's asynchronous I/O facilities ; 从libuv书中拿走的重要一点是libuv使用主机操作系统的异步 I / O 工具 ; it does not simply create a new thread for every connection. 它不是简单地为每个连接创建一个新线程

libuv tells the OS that it would like to know about any changes (connection state, data received, etc) that happen on a particular set of sockets. libuv告诉操作系统,它想知道在特定套接字上发生的任何更改(连接状态,接收的数据等)。 It is then up to the OS to deal with managing the connections. 然后由OS来处理管理连接。 The OS itself may create one or more threads to accomplish that, but that's not our concern. 操作系统本身可以创建一个或多个线程来实现这一点,但这不是我们关注的问题。 (And it certainly won't create a thread for every connection.) (它肯定不会为每个连接创建一个线程。)

For other types of operations like calls to C libraries that may be computationally expensive (ie crypto), libuv provides a thread pool on which those operations may run. 对于其他类型的操作,例如对可能计算成本很高的C库的调用(即加密),libuv提供了一个线程池 ,可以在其上运行这些操作。 Since it is a thread pool, again you don't have to worry about thread count growing without bound. 由于它是一个线程池,因此您不必担心线程计数在没有绑定的情况下增长。 When the pool is fully busy, operations are queued. 当池完全忙碌时,操作会排队。

So yes, JavaScript runs on a single thread. 所以是的,JavaScript运行在一个线程上。 Yes, node (via libuv) spawns many threads in the background to do work so that it need not block the JavaScript thread. 是的,node(通过libuv)在后台产生许多线程来完成工作,因此它不需要阻止JavaScript线程。 However, the thread count is always controlled, and I/O generally doesn't even get its own node-allocated thread because that's handled by the OS. 但是,线程计数始终受到控制,并且I / O通常甚至不会获得自己的节点分配线程,因为这是由OS处理的。

Alright, let's break this down a bit. 好吧,让我们稍微打破一下。 Single threaded applications have advantages: you can never get deadlocks or race conditions. 单线程应用程序具有以下优点:您永远不会遇到死锁或竞争条件。 These issues stem from simultaneous memory access in multi-threaded systems. 这些问题源于多线程系统中的同时内存访问。 If two threads access the same piece of information weird things can happen. 如果两个线程访问同一条信息,可能会发生奇怪的事情。

So why does JavaScript have Workers? 那么为什么JavaScript有工人呢? If you need do some heavy processing you're going to block the event loop, you could try to split up the workload by generating timer events but that's tedious. 如果你需要做一些繁重的处理,你将阻止事件循环,你可以尝试通过生成计时器事件来分散工作负载,但这很乏味。 A Worker allows you to spawn a thread under one condition: no shared memory access . Worker允许您在一个条件下生成一个线程: 没有共享内存访问 This solves the issue of heavy processing in a single threaded environment while avoiding the pitfalls of multi-threaded environments (deadlocks, race-conditions). 这解决了单线程环境中繁重处理的问题,同时避免了多线程环境(死锁,竞争条件)的陷阱。

And as @dandavis said, if you have a multi-core CPU (which everyone does these days) the Worker threads can be offloaded to the other cores. 正如@dandavis所说,如果你有一个多核CPU(现在每个人都这么做),可以将Worker线程卸载到其他核心。

You have to appreciate that, although JavaScript is single threaded, the environment around it is still very much multi-threaded. 你必须要明白,虽然JavaScript是单线程的,但它周围的环境仍然是非常多线程的。 Reading a file is non-blocking in Node.JS but there is likely a thread to support it in the OS. 在Node.JS中读取文件是非阻塞的,但可能有一个线程在操作系统中支持它。


As a minor addendum I would say that Node.JS's biggest advantage is that it allows you to write JavaScript on the server, which allows you to share code between the client and the server. 作为一个小的附录我会说Node.JS的最大优点是它允许你在服务器上编写JavaScript,这允许你在客户端和服务器之间共享代码 The fact that it's non-blocking is nice but threads already solve that. 事实上它是非阻塞的很好但是线程已经解决了这个问题。 The non-blocking IO stems from the single threaded-ness. 非阻塞IO源于单线程。 It's very inconvenient to have a single thread with blocking IO. 拥有阻塞IO的单个线程非常不方便。

The fact that Node.js does not need to spin a new thread to handle each web request becomes an advantage in environments where you have a very large number of concurrent connections (as in thousand or tens of thousands of concurrent connections.) Below that you are probably not going to see any significant difference with a threaded environment like Java/Apache or C#/IIS given that those platforms are very well optimized these days. Node.js不需要旋转新线程来处理每个Web请求这一事实在您拥有大量并发连接的环境中变得很有优势(如数千或数万个并发连接。)在您之下可能不会看到与Java / Apache或C#/ IIS等线程环境有任何显着差异,因为这些平台目前已经得到了很好的优化。

The advantage is that even if Node.js were to spin a new thread for I/O operations (which it might in certain cases but not in others) you are still reducing the number of threads needed to handle the web request from 1-per-request to 1. Again, if you are processing thousands of concurrent connections that's a huge gain. 优点是,即使Node.js为I / O操作(在某些情况下可能在某些情况下而不在其他情况下)旋转新线程,您仍然可以减少处理Web请求所需的线程数量,从1-per - 再次请求1.如果你正在处理成千上万的并发连接,那将是一个巨大的收获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node js是单线程还是多线程? - Node js single threaded or multi-threaded? Node.js 中的多个 setTimeout 执行是单线程还是多线程? - is Multiple setTimeout execution single threaded or mutli threaded in Node.js? 为什么 IO 的非阻塞异步单线程比某些应用程序的阻塞多线程更快 - Why is Non blocking asynchronous single-threaded faster for IO than blocking multi-threaded for some applications JS如何既非阻塞/异步又是单线程的? - How is JS both non-blocking / asynchronous but single-threaded? 为什么 node.js 在单线程时速度很快? - Why node.js is fast when it's single threaded? Node JS单线程Vs MultiThreading(CPU利用率:有什么不同?) - Node JS single Threaded Vs MultiThreading (CPU Utilization : Any difference ? ) NodeJS是单线程的-在无限循环的情况下仍然不会阻塞 - NodeJS is Single Threaded - still not blocking in case of infinite loop JavaScript 中的事件循环和 Node.js 中的异步非阻塞 I/O 有什么区别? - What is the difference between the event loop in JavaScript and async non-blocking I/O in Node.js? 单线程Node.js如何同时处理请求? - How does single-threaded Node.js handles requests concurrently? Node.js - 单线程,非阻塞? - Node.js - single thread, non-blocking?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM