Blocking I/O calling thread को तब तक रोक देता है जब तक operation समाप्त न हो जाए; non-blocking I/O तुरंत return करता है और thread को operation पूरा होने के दौरान अन्य कार्य करने देता है। Sync बनाम async API स्तर पर उसी विचार का वर्णन करता है।
Blocking I/O calling thread को तब तक रोक देता है जब तक operation समाप्त न हो जाए; non-blocking I/O तुरंत return करता है और thread को operation पूरा होने के दौरान अन्य कार्य करने देता है। Sync बनाम async API स्तर पर उसी विचार का वर्णन करता है।
read() पर तब तक रुका रहता है जब तक data न आ जाए। सरल, लेकिन thread पूरे समय निष्क्रिय रहता है — N धीमे clients की सेवा के लिए आपको N threads चाहिए।Blocking: ──[ wait for disk ]── (thread stuck, does nothing)
Non-blocking: ──▶ returns now; event loop notified when ready
// Blocking: nothing else runs until the file is read
const data = fs.readFileSync('big.txt');
// Non-blocking: thread keeps going; callback fires later
fs.readFile('big.txt', (err, data) => { /* handle later */ });
// or async/await on top of non-blocking primitives
const data2 = await fs.promises.readFile('big.txt');
यह scalable servers का मूल है। Node.js और nginx कम threads पर विशाल connection counts संभालते हैं ठीक इसलिए क्योंकि I/O non-blocking है — प्रतीक्षा करना एक thread को नहीं बाँधता। scripts में सरलता के लिए blocking calls का उपयोग करें; high-concurrency network services के लिए non-blocking का उपयोग करें, और CPU-भारी कार्य से event loop को कभी block न करें।
विस्तृत उत्तरों के साथ IT इंटरव्यू प्रश्नों की एक लाइब्रेरी — जूनियर से सीनियर तक।
दान करें