• Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials
      • InfyTQ Archive
      • Infosys Archive
      • TCS Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Tutorials
      • Node.js Tutorial
      • Express.js Tutorial
      • Python Tutorial
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
      • Java Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements

Node.js follows non-blocking I/O model, Know why

  • July 6, 2023
  • CODE OF GEEKS
  • 0
Node.js follows non-blocking I/O model
Node.js follows non-blocking I/O model

Non-blocking refers to the ability of a system or programming model to perform tasks without waiting for the completion of previous tasks.

In the context of I/O (Input/Output) operations, non-blocking operations allow a program to continue executing and performing other tasks while waiting for I/O operations to complete.

This allows for better utilization of system resources and improved responsiveness in handling multiple concurrent operations.

In a non-blocking I/O model, when an I/O operation is initiated, the program does not wait for the operation to complete before moving on to the next task. Instead, it continues executing other tasks or operations that are ready to be processed. The I/O operation is performed asynchronously in the background, and the program can be notified when the operation is completed or can periodically check for its completion.

Non-blocking I/O is particularly beneficial in scenarios where there may be delays in I/O operations, such as reading from files, making network requests, or accessing databases. By not blocking the execution flow, the program can perform other tasks or respond to events while waiting for I/O operations to finish. This approach allows for more efficient resource utilization and helps prevent the program from becoming unresponsive or experiencing long delays.

Node.js, being built on the non-blocking, event-driven architecture, excels in handling non-blocking I/O operations. It leverages the single-threaded event loop to manage and handle multiple concurrent operations efficiently, making it well-suited for building scalable and high-performance applications that involve heavy I/O operations or concurrent connections.



Simple example to demonstrate the non-blocking I/O nature of Node.js:

Consider the following code snippet:

const fs = require('fs');

console.log('Before File Read');

fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  console.log('File Contents:', data);
});

console.log('After File Read');

In this example, we’re using the fs module in Node.js to read the contents of a file named example.txt. Let’s assume that the file exists and contains some text.

Now, when we run this code, the output will be:

Before File Read
After File Read
File Contents: <file data>

But, But, why is it not like the below one ?

Before File Read
File Contents: <file data>
After File Read

This is what truly the concept of non-blocking says.

Here’s how the non-blocking I/O works in this example:

a. The code execution starts with the first console.log(‘Before File Read’).

b. The fs.readFile() function is invoked, which initiates the file reading process. However, Node.js doesn’t block the execution here and continues to the next line of code.

c. The second console.log(‘After File Read’) is executed immediately without waiting for the file reading operation to complete.

d. Meanwhile, Node.js continues to process other tasks and events in the event loop.

e. Once the file reading operation is completed, the callback function provided to fs.readFile() is called with the file contents.

f. The callback function prints the file contents with console.log(‘File Contents:’, data).

From this example, we can observe that:

  • The code execution doesn’t wait for the file reading operation to finish.
  • Instead, it continues executing the next lines of code immediately, demonstrating the non-blocking nature of Node.js.
  • When the file reading operation is completed, the provided callback function is called asynchronously, allowing the code to handle the result.

This behavior allows Node.js to efficiently handle multiple concurrent operations without blocking the execution flow. It enables applications to handle large numbers of connections and perform other tasks while waiting for I/O operations to complete.

This example demonstrates how Node.js utilizes non-blocking I/O to achieve high performance and efficiency in handling I/O operations asynchronously.





  • Previous Finding the Sum of Digits of a Number in Python with explanation
  • Next Comprehensive Node.js Tutorial: From Beginner to Advanced with Practical Examples

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Advertise with us

More on Node.js

  • Introduction to Node.js
  • NPM in Node.js
  • Non-Blocking Nature of Node.js
  • First Application with Node.js
  • Node.JS Modules
  • HTTP Module Node.js
  • Reading File with Node.js
  • Writing File with Node.js
  • Creating File with Node.js
  • Updating File with Node.js
  • Deleting File with Node.js
  • Renaming File with Node.js
  • URL Module in Node.js
  • Event Programming with Node.js
  • async-await in Node.js
  • Buffer in Node.js
  • Sending Mails with Node.js
  • Connecting to MySQL(CRUD) in Node.js

More on Express.js

  • Introduction to Express.js
  • Creating First App with Express.js
  • Routes in Express.js
  • Middlewares in Express.js
  • Web App with Express & Pug
  • Web App with Express & EJS
  • Serving Static Files with Express.js
  • Handling Forms with Express.js
  • Handling File uploads in forms
  • Working with Cookies in Express.js
  • Session Handling with Express.js
  • REST API with Express.js
  • Validating Requests in Express.js
  • Fetch data from External API with axios
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions
Refund Policy
Contact us

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Tutorials

Python
Node.js
Express.js
Golang

Recent Posts

Strings in Go and its basic operations
  • August 11, 2023
Introduction to Functions in Go programming
  • August 11, 2023

Copyright @ CODE OF GEEKS. All Rights Reserved.