If you don’t open PM2 or Cluster, try an express Node website. So it means that only a single thread is processing Node’s requests. now there are many people requesting together. will these people queue up to process one by one? If there is asynchronous code in the middle of the logic, what is Node doing at this time before callback (Io is in progress, Central Processor is not consumed, Node is waiting)? It is unlikely that Node will handle other requests during Io, right?
Is to handle other requests.
The main thread of Node.js will not be in the block state unless the synchronization method of node api inside such as fs.readFileSync is used.
Node.js uses Reactor mode. whenever it encounters a block, it either uses the system’s asynchronous API (network request) or throws it into the thread pool (file read and write), and the main thread then processes other requests.
Briefly describe the working sequence of the Node.js main thread:
Timer when executed
Check whether the asynchronous operation queue inside has completed and execute callback
Check for new connections
Back to 1
So when multiple requests are concurrent, Node.js is essentially queuing up, but everyone’s waiting time is very short, unless each request consumes a lot of Central Processor time. Generally speaking, web server is waiting for IO (network request) most of the time, so Node.js, a Reactor mode server, is relatively efficient for Central Processor utilization, avoiding Central Processor context switching caused by thread switching.