Blocking I/O operation પૂરું ન થાય ત્યાં સુધી calling thread ને અટકાવે છે; non-blocking I/O તરત જ return કરે છે અને operation પૂરું થાય ત્યાં સુધી thread ને બીજું કામ કરવા દે છે. Sync vs async એ જ વિચારને API સ્તરે વર્ણવે છે.
Blocking I/O operation પૂરું ન થાય ત્યાં સુધી calling thread ને અટકાવે છે; non-blocking I/O તરત જ return કરે છે અને operation પૂરું થાય ત્યાં સુધી thread ને બીજું કામ કરવા દે છે. Sync vs async એ જ વિચારને API સ્તરે વર્ણવે છે.
read() પર અટકે છે. સરળ, પણ thread આખો સમય નિષ્ક્રિય રહે છે — N ધીમા clients ને serve કરવા તમને 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 વાપરો, અને event loop ને ક્યારેય CPU-ભારે કામથી block ન કરો.
વિગતવાર જવાબો સાથે IT ઇન્ટરવ્યૂ પ્રશ્નોની લાઇબ્રેરી — જુનિયરથી સિનિયર સુધી.
દાન કરો