我不是Node程序员,但我对单线程非阻塞IO模型如何工作感兴趣。 在我阅读了理解node-js-event-loop这篇文章之后,我真的很困惑。 文中给出了模型的一个例子:
c.query(
'SELECT SLEEP(20);',
function (err, results, fields) {
if (err) {
throw err;
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><head><title>Hello</title></head><body><h1>Return from async DB query</h1></body></html>');
c.end();
}
);
Que: When there are two requests A(comes first) and B since there is only a single thread, the server-side program will handle the request A firstly: doing SQL querying is asleep statement standing for I/O wait. And The program is stuck at the I/O waiting, and cannot execute the code which renders the web page behind. Will the program switch to request B during the waiting? In my opinion, because of the single thread model, there is no way to switch one request from another. But the title of the example code says that everything runs in parallel except your code.
(注:我不确定我是否误解了代码,因为我有 从未使用过Node。)在等待过程中,节点如何将A切换到B ?并且可以 你解释了Node的单线程非阻塞IO模型 简单的方法吗?如果你能帮助我,我会很感激的。:)