Node.js / express: respond immediately to client request and continue tasks in nextTick

前端 未结 3 2059
情歌与酒
情歌与酒 2021-02-06 09:20

I would like to separate server high consuming CPU task from user experience:

./main.js:

var express = require(\'express\');
var Test = require(\'./resou         


        
3条回答
  •  忘了有多久
    2021-02-06 10:23

    Try waiting instead of hogging the CPU:

    res.send("Hello world, this should be sent inmediately");
    console.log("Response sent.");
    setTimeout(function() {
      console.log("After-response code running!");
    }, 3000);
    

    node.js is single-threaded. If you lock up the CPU with a busy loop, the whole thing grinds to a halt until that is done.

提交回复
热议问题