|
2 | 2 |
|
3 | 3 | [The official Doc](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/) has some very good explanation.
|
4 | 4 |
|
5 |
| -## 1> Some Key points noted on 10-June-2018 >> |
| 5 | +## 1> Some Key points >> |
6 | 6 |
|
7 | 7 | - A> The simple ans is - With callback function and event-loop
|
8 | 8 |
|
9 |
| -- B> With callback function and event-loop, Node transfer execution of the callback function (like, in setTimeout, Promise, fs.readFile) to a queue the output of which follows a FIFO design, while the rest of the code, will get executed. So that’s how its node is non-blocking. |
| 9 | +- B> With callback function and event-loop, Node transfer execution of the callback function (like, in setTimeout, Promise, fs.readFile) to a queue the output of which follows a FIFO design, while the rest of the code, will get executed. So that’s how node is non-blocking. |
10 | 10 |
|
11 |
| -And whatever was transferred to the separate queue for those callbacks will be executed on a FIFO basis, so an the code that was in the queue first, will return its result first. And then the next one in the queue. |
| 11 | +And whatever was transferred to the separate queue for those callbacks will be executed on a FIFO basis, so the code that was in the queue first, will return its result first. And then the next one in the queue. |
12 | 12 |
|
13 | 13 | ## 2> https://codeburst.io/how-node-js-single-thread-mechanism-work-understanding-event-loop-in-nodejs-230f7440b0ea
|
14 | 14 |
|
15 |
| -Node is single threaded and it is doing magical things with this model. Some of the popular server side technology like PHP, ASP.NET, Ruby & Java Servers all follow Multi-threaded where each client request results in the instantiation of a new thread or even a process, but in Node.js, requests are run on the same thread The main event loop is single-threaded but most of the I/O works run on separate threads, because the I/O APIs in Node.js are asynchronous/non-blocking by design, in order to accommodate the event loop. |
| 15 | +Some of the popular server side technology like PHP, ASP.NET, Ruby & Java Servers all follow Multi-threaded where each client request results in the instantiation of a new thread or even a process, but in Node.js, requests are run on the same thread. |
| 16 | + |
| 17 | +##### Misconception 1: The event loop runs in a separate thread than the user code - |
| 18 | +Many think, there is a main thread where the JavaScript code of the user (userland code) runs in and another one that runs the event loop. Every time an asynchronous operation takes place, the main thread will hand over the work to the event loop thread and once it is done, the event loop thread will ping the main thread to execute a callback. |
| 19 | + |
| 20 | +#### Reality |
| 21 | +**There is only one thread that executes JavaScript code AND ALSO the the event loop. The execution of callbacks (know that every userland code in a running Node.js application is a callback) is done by the event loop.** |
| 22 | + |
| 23 | + |
| 24 | +### Event Loop vs Worker Pool (Thread Pool) |
| 25 | + |
| 26 | +[https://nodejs.org/ja/docs/guides/dont-block-the-event-loop/(https://nodejs.org/ja/docs/guides/dont-block-the-event-loop/) |
| 27 | + |
| 28 | +Node uses a small number of threads to handle many clients. In Node there are two types of threads: one Event Loop (aka the main loop, main thread, event thread, etc.), and a pool of k Workers in a Worker Pool (aka the threadpool). Node uses the Event-Driven Architecture: it has an Event Loop for orchestration and a Worker Pool for expensive tasks. |
| 29 | +Event Loop executes the JavaScript callbacks registered for events, and is also responsible for fulfilling non-blocking asynchronous requests like network I/O. |
| 30 | + |
| 31 | +#### What code runs on the Worker Pool? |
| 32 | + |
| 33 | +Node's Worker Pool is implemented in libuv (docs), which exposes a general task submission API. |
| 34 | + |
| 35 | +Node uses the Worker Pool to handle "expensive" tasks. This includes I/O for which an operating system does not provide a non-blocking version, as well as particularly CPU-intensive tasks. |
| 36 | + |
| 37 | +Event loop is the mechanism that takes callbacks (functions) and registers them to be executed at some point in the future. It operates in the same thread as the proper JavaScript code. When a JavaScript operation blocks the thread, the event loop is blocked as well. |
| 38 | + |
| 39 | +Worker pool is an execution model that spawns and handles separate threads, which then synchronously perform the task and return the result to the event loop. The event loop then executes the provided callback with said result. |
| 40 | + |
| 41 | +=============================== |
16 | 42 |
|
17 | 43 | ## 3> Single Threaded Event Loop Model Processing Steps:
|
18 | 44 |
|
@@ -131,4 +157,7 @@ Node has a pool of Thread and you must be scratching your head wondering if Node
|
131 | 157 |
|
132 | 158 | Generally the web server and the database server are 2 different machines, because of Async nature, the event loop gets free after forwarding the read/write request to database server. That is why, a Node JS HTTP server can handle a large number of requests while the process of complex read/write operations could be inprocess on database server(s).
|
133 | 159 |
|
134 |
| -### Further Resources - https://youtu.be/8aGhZQkoFbQ - Very famous video |
| 160 | +### |
| 161 | + |
| 162 | +- 1. Further Resources - https://youtu.be/8aGhZQkoFbQ - Very famous video |
| 163 | +- 2. [https://nodejs.org/ja/docs/guides/dont-block-the-event-loop/](https://nodejs.org/ja/docs/guides/dont-block-the-event-loop/) |
0 commit comments