Тёмный

How NodeJS works behind the scenes | Event Loop | Multi-threading | Non-Blocking tasks | FreshTutorz 

Fresh Tutorz മലയാളം
Подписаться 391
Просмотров 239
50% 1

Node.js achieves a non-blocking main thread through a combination of the event loop and asynchronous programming techniques. Here's a deeper dive into how it works:
*The Event Loop in Action:*
1. *Initiating an Asynchronous Operation:* When you call a function for an I/O operation (like reading a file or making a network request) in Node.js, it doesn't wait for that operation to finish. Instead, the operation is initiated and then:
- A callback function is registered with the event loop. This callback will be executed later when the I/O operation completes.
- The main thread continues executing other code.
2. *Event Queues and Processing:* The event loop maintains different queues for various event types. When an asynchronous operation is initiated, the corresponding callback is added to its specific queue (e.g., network I/O queue).
3. *Non-Blocking I/O:* The key here is that Node.js doesn't use separate threads for these I/O operations. Instead, it relies on the operating system's mechanisms for handling I/O efficiently. The main thread doesn't get blocked waiting for the I/O to complete.
4. *Event Loop Takes Over:* Once the I/O operation finishes, the operating system notifies the event loop. The event loop then retrieves the relevant callback function from the appropriate queue.
5. *Callback Execution:* The retrieved callback function is placed on the call stack of the main thread. When the main thread finishes executing the current code, it fetches the callback from the call stack and executes it.
*Benefits of Non-Blocking Main Thread:*
- *Scalability:* By not blocking the main thread on I/O, Node.js can handle a large number of concurrent connections efficiently. This makes it suitable for applications that deal with many users or network requests.
- *Responsiveness:* Since the main thread remains free, Node.js applications can remain responsive even when handling multiple I/O operations. Users won't experience delays because of long-running I/O tasks.
*Important Note:*
It's important to remember that Node.js is single-threaded. While the event loop manages to keep the main thread free for most tasks, if you have long-running synchronous operations in your code, they can still block the event loop and hinder responsiveness. So, it's essential to structure your Node.js applications using asynchronous patterns and leverage the event loop effectively for optimal performance.
*Single-Threaded with Event Loop:*
- At its core, Node.js uses a single-threaded event loop mechanism to manage asynchronous operations. This event loop efficiently handles tasks like I/O operations (file access, network requests) without blocking the main thread.
*Non-Blocking I/O:*
- The key to Node.js's concurrency lies in its non-blocking I/O model. When an asynchronous operation is initiated, the main thread doesn't wait for it to finish. Instead, it moves on to execute other code.
*Event Queues and Callbacks:*
- The event loop manages queues for different event types. When an asynchronous operation starts, a callback function is registered with the event loop. This callback will be executed later when the I/O is complete.
- The event loop continuously monitors these queues and the operating system for I/O completion signals.
*Event Loop Takes Over:*
- Once an I/O operation finishes, the OS notifies the event loop. The event loop then retrieves the corresponding callback function from the relevant queue.
*Callback Execution:*
- The retrieved callback is placed on the main thread's call stack. When the main thread finishes its current task, it fetches the callback and executes it, handling the completed I/O operation.
*Benefits of Single-Threaded with Event Loop:*
- *Scalability:* By not blocking the main thread, Node.js can efficiently handle a high volume of concurrent connections, making it suitable for applications with many users or network requests.
- *Responsiveness:* Since the main thread remains free for most tasks, Node.js applications can stay responsive even while dealing with multiple I/O operations.
*Considering Worker Threads:*
- While Node.js is primarily single-threaded, it offers the `worker_threads` module for utilizing multiple threads for CPU-bound tasks. This can be beneficial for computationally expensive operations that would otherwise block the main thread. However, it's important to note that worker threads are not suitable for all scenarios and can introduce complexities.
🔗 Subscribe here: / @freshtutorzmalayalam
Don't forget to hit the notification bell 🔔 so you never miss an update! Your support means the world to us. 🌎✨
📌 Connect with us:
Instagram: / freshtutorz
Facebook: / freshtutorz
Thanks for being part of the Fresh Tutorz family! 🤗💙 #Subscribe #freshtutorzmalayalam #CommunityLove

Опубликовано:

 

12 май 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 6   
@princepr5830
@princepr5830 Месяц назад
Thanks for the good explanation 🎉
@rijoksd
@rijoksd Месяц назад
@abdullahet2566
@abdullahet2566 Месяц назад
Good information ❤
@Freshtutorzmalayalam
@Freshtutorzmalayalam Месяц назад
Thank you 🙏
@rahulramachandran1002
@rahulramachandran1002 Месяц назад
Bro... Need more videos... ❤️
@Freshtutorzmalayalam
@Freshtutorzmalayalam Месяц назад
Sure bro.. I'll try my best
Далее
OAuth 2.0 explained with examples
10:03
Просмотров 103 тыс.
Me: Don't cross there's cars coming
00:16
Просмотров 6 млн
Rank Push live | Thunder Op
Просмотров 28