admin管理员组文章数量:1336711
I'm having trouble understanding exactly how process.nextTick
does its thing. I thought I understood, but I can't seem to replicate how I feel this should work:
var handler = function(req, res) {
res.writeHead(200, {'Content-type' : 'text/html'});
foo(function() {
console.log("bar");
});
console.log("received");
res.end("Hello, world!");
}
function foo(callback) {
var i = 0;
while(i<1000000000) i++;
process.nextTick(callback);
}
require('http').createServer(handler).listen(3000);
While foo
is looping, I'll send over several requests, assuming that handler
will be queued several times behind foo
with callback
being enqueued only when foo
is finished.
If I'm correct about how this works, I assume the oute will look like this:
received
received
received
received
bar
bar
bar
bar
But it doesn't, it's just sequential:
received
bar
received
bar
received
bar
received
bar
I see that foo
is returning before executing callback
which is expected, but it seems that callback
is NEXT in line, rather than at the end of the queue, behind all of the requests ing in. Is that the way it works? Maybe I'm just not understanding how exactly the event queue in node works. And please don't point me here. Thanks.
I'm having trouble understanding exactly how process.nextTick
does its thing. I thought I understood, but I can't seem to replicate how I feel this should work:
var handler = function(req, res) {
res.writeHead(200, {'Content-type' : 'text/html'});
foo(function() {
console.log("bar");
});
console.log("received");
res.end("Hello, world!");
}
function foo(callback) {
var i = 0;
while(i<1000000000) i++;
process.nextTick(callback);
}
require('http').createServer(handler).listen(3000);
While foo
is looping, I'll send over several requests, assuming that handler
will be queued several times behind foo
with callback
being enqueued only when foo
is finished.
If I'm correct about how this works, I assume the oute will look like this:
received
received
received
received
bar
bar
bar
bar
But it doesn't, it's just sequential:
received
bar
received
bar
received
bar
received
bar
I see that foo
is returning before executing callback
which is expected, but it seems that callback
is NEXT in line, rather than at the end of the queue, behind all of the requests ing in. Is that the way it works? Maybe I'm just not understanding how exactly the event queue in node works. And please don't point me here. Thanks.
2 Answers
Reset to default 3process.nextTick put the callback on the next tick that is going to be executed, not at the end of the tick queue.
Node.js doc (http://nodejs/api/process.html#process_process_nexttick_callback) say: "It typically runs before any other I/O events fire, but there are some exceptions."
setTimeout(callback, 0) will probably work more like you describe.
You should certainly read the link fgascon provided, and perhaps
https://github./joyent/node/issues/3335 for more background.
Use process.nextTick for when you want to call some code before any IO, but after the calling context has returned (usually because you want to register listeners on an event emitter and need to return the created emitter before you can register anything).
本文标签: javascriptunderstanding the nodejs event queue and processnextTickStack Overflow
版权声明:本文标题:javascript - understanding the node.js event queue and process.nextTick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742412966a2470155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论