Single threaded event loop against multi-threaded non-blocking working device in Node.JS

Node. The biggest advantage is the non-blocking nature. It is single-threaded, so it does not need to create a new stream for each new incoming connection.

Behind the contour of the event (which is actually single-threaded) there is a "Non blocking Worker". This thing is no longer single, so (as I understand it) it can spawn a new thread for each task.

Maybe I didn’t understand something, but where exactly is the advantage. If there are many tasks to handle, will a non-blocking worker turnover work with a blocking worker?

Thanks Christian

+7
javascript nonblocking
Jan 31 '14 at 17:08
source share
3 answers

You need to read about libuv , the “magic” behind the non-blocking node I / O.

The important thing to remove from the libuv book is that libuv uses the OS host asynchronous I / O objects ; it does not just create a new thread for each connection.

libuv tells the OS that he would like to know about any changes (connection status, received data, etc.) that occur in a particular set of sockets. It is then that the operating system copes with connection management. The OS itself can create one or more threads for this, but this is not our problem. (And this, of course, will not create a thread for each connection.)

For other types of operations, such as calls to C libraries, which can be expensive (e.g. crypto), libuv provides a thread pool on which these operations can be performed. Since this is a thread pool, again, you don’t have to worry about the number of threads growing unlimited. When the pool is fully occupied, operations are queued.

So yes, JavaScript runs on a single thread. Yes, node (via libuv) creates a lot of threads in the background to do the job, so there is no need to block the JavaScript stream. However, the thread counter is always monitored, and I / O usually does not even get its own node-distributed thread, because it is handled by the OS.

+27
Jan 31 '14 at 19:04
source share

Well, let it break a little. Single-threaded applications have advantages: you can never get deadlocks or race conditions. These problems are associated with simultaneous memory access in multi-threaded systems. If two threads access the same piece of information, strange things can happen.

So why does JavaScript have Workers? If you need some kind of heavy processing, you are going to block the event loop, you can try to split the workload by creating timer events, but this is tedious. A worker allows you to create a thread under one condition: without access to shared memory . This solves the problem of intensive processing in a single-threaded environment, while avoiding the traps of multi-threaded environments (dead ends, race conditions).

And as @dandavis said, if you have a multi-core processor (which everyone does these days), worker threads can be offloaded to other cores.

You should appreciate this, although JavaScript is single-threaded, the environment is still very multi-threaded. Reading the file is not blocked in Node.JS, but there is probably a thread to support it in the OS.




As a minor addition, I would say that the greatest advantage of Node.JS is that it allows you to write JavaScript on the server, which allows you to share code between the client and server. The fact that it is non-blocking is good, but threads are already solving this. Non-blocking IO stems from a single threaded. It is very inconvenient to have one thread with an IO lock.

+4
Jan 31 '14 at 17:11
source share

The fact that Node.js does not need to spin a new thread to process each web request becomes an advantage in environments where you have a lot of simultaneous connections (for example, thousands or tens of thousands of parallel connections.) Below you are unlikely to see a significant difference with a streaming environment such as Java / Apache or C # / IIS, given that these platforms are very well optimized these days.

The advantage is that even if Node.js had to spin a new thread for I / O operations (which may be in some cases, but not in others), you still reduce the number of threads required to work with the network request from 1 -a request to 1. Again, if you process thousands of simultaneous connections that have huge payoffs.

+1
Jan 31 '14 at 18:29
source share



All Articles