开发者

nodejs and the non-blocking nightmare

I'm currently developing an API using node.js and MySQL. I'm new to this non-blocking stuff, and I have a question. I'm using node and MySQL module.

Say that we have a function like this:

function doQuery(sql, callback) {
    connect(); //does the Client.connect()
    client.query(sql, function(err, results, fields) {
        if (err) {
            errorLog.trace(err, __filename);
            throw err;
        } else {
            logger.trace('DATABASE ACCESS: {query: ' + sql + '} result: OK', __filename);
        }
        client.end();

        callback(results);
    });
}

Everything runs ok, callbac开发者_C百科k handles the return of the values, but there's something that bothers me. My browser what till the response is back and i don't know if this is because during this time node is actually blocked, or not.

So, how can I know if an operation is actually blocking my node process? I thought that when you pass a callback to a function, node automatically handles it and puts the execution of this callback at the queue of the event loop. But I'm not actually sure about that

Does all this make any sense to you?


There is a difference between the browser waiting and node.js blocking.

The browser has to wait because it can't get the data back instantly. The browser will stop waiting once you send the response back. Just because the browser is waiting doesn't mean that node.js is blocking. It just means that the connection is still open

Node.js idles whilst your waiting for the callback. it does not block.

node.js can have thousands of open connections with browsers clients. This does not mean it's blocking on each one. It simply means that is idling until it has a callback to handle or a new request to handle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜