I can write non-blocking [I/O][1] in [Node.js ][2] very easily. It's what the entire library is set up for.
But any computation done is blocking. Any message passing over [event emitters are blocking][3].
For example, emitting events are resolved immediately and are thus blocking:
var e = new process.EventEmitter;
e.on("foo", function() {
console.log("event");
});
process.nextTick(function() {
console.log("next tick");
});
setTimeout(function() {
console.log("timeout");
}, 0);
e.emit("foo");
> event
> next tick
> timeout
Apart from wrapping calls in `nextTick`, how do I make code non-blocking?
I want to do as little computation per cycle of the event loop as possible, so that I can serve as many clients simultaneously as possible.
How do I write my code in a non-blocking fashion?
And when I have non-blocking code, how do I scale that across multiple processes?
One option is waiting for the WebWorker sub-process API to be finished.
[1]: http://en.wikipedia.org/wiki/Input/output
[2]: http://en.wikipedia.org/wiki/Node.js
[3]: https://github.com/joyent/node/blob/bc8489580cbc36eca511988ca134326b6ec2e2cc/lib/events.js#L38
Firstly, 90% of your 'Question' is not actually a question, it's more of an issue with node's Event Library, this should be brought up either as a feature request or as a possible bug on github, as for your small question I would create a question dedicated to that subject rather then squeezing it in this one.
以上就是How do I write non-blocking code in Node.js?的详细内容,更多请关注web前端其它相关文章!