Blocking I/O ले operation सकिनेसम्म calling thread रोक्छ; non-blocking I/O तुरुन्तै फर्किन्छ र operation पूरा हुँदै गर्दा thread लाई अरू काम गर्न दिन्छ। Sync बनाम async ले उही विचारलाई API तहमा वर्णन गर्छ।
Blocking I/O ले operation सकिनेसम्म calling thread रोक्छ; non-blocking I/O तुरुन्तै फर्किन्छ र operation पूरा हुँदै गर्दा thread लाई अरू काम गर्न दिन्छ। Sync बनाम async ले उही विचारलाई API तहमा वर्णन गर्छ।
read() मा पार्क हुन्छ। सरल, तर 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 अन्तर्वार्ता प्रश्नहरूको पुस्तकालय — जुनियरदेखि सिनियरसम्म।
दान गर्नुहोस्