What happens when a single request takes a long time with these non-blocking I/O servers?

前端 未结 6 1195
北恋
北恋 2021-01-12 00:59

With Node.js, or eventlet or any other non-blocking server, what happens when a given request takes long, does it then block all other requests?

Example, a request c

6条回答
  •  盖世英雄少女心
    2021-01-12 01:25

    Here is some code to actually see the blocking / non-blocking in action:

    • With this example (long CPU-computing task, non I/O):

      var net = require('net');
      handler = function(req, res) {
          console.log('hello');
          for (i = 0; i < 10000000000; i++) { a = i + 5;  }
      }
      net.createServer(handler).listen(80);
      

      if you do 2 requests in the browser, only a single hello will be displayed in the server console, meaning that the second request cannot be processed because the first one blocks the Node.js thread.

    • If we do an I/O task instead (write 2 GB of data on disk, it took a few seconds during my test, even on a SSD):

      http = require('http');
      fs = require('fs');
      buffer = Buffer.alloc(2*1000*1000*1000);
      first = true;
      done = false;
      
      write = function() {
          fs.writeFile('big.bin', buffer, function() { done = true; });
      }
      
      handler = function(req, res) {
          if (first) {
              first = false;
              res.end('Starting write..')
              write();      
              return;
          }
          if (done) { 
              res.end("write done."); 
          } else {  
              res.end('writing ongoing.'); 
          }
      }
      
      http.createServer(handler).listen(80);
      

      here we can see that the a-few-second-long-IO-writing-task write is non-blocking: if you do other requests in the meantime, you will see writing ongoing.! This confirms the well-known non-blocking-for-IO features of Node.js.

提交回复
热议问题