我可以在 I/O 中编写非阻塞 Node.js 非常容易。这就是整个图书馆的目的。
但任何完成的计算都是阻塞的。任何通过事件发射器的消息都会被阻塞。
例如,发出事件会立即解决,因此会阻塞:
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
除了将调用包装在 nextTick
中,如何使代码非阻塞?
我希望在事件循环的每个周期中进行尽可能少的计算,以便我可以同时为尽可能多的客户端提供服务。
如何以非阻塞方式编写代码?
当我有非阻塞代码时,如何跨多个进程扩展它?
一种选择是等待 WebWorker 子流程 API 完成。
I can write non-blocking I/O in Node.js 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.
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.
发布评论
评论(2)
JavaScript 是单线程的。这意味着无论事件、超时或 nextTick 延迟如何,完成的任何计算都会阻塞整个过程。
如果您使用
process.nextTick
将处理分步骤进行,就像在客户端使用setTimeout(fn, 0)
所做的那样,以避免阻塞 UI,您可能会传播您的工作负载在较长的时间跨度内,为其他功能的运行提供了一些空间。但这是一个非常无效的解决方案 - 总工作量是相同的,分布在所有周期中(使每个请求稍微慢一些)。在实践中,任何预计需要超过几毫秒的计算都应该卸载到不同的进程。为了最大化并发性,您应该始终尽快返回到事件循环。
child_process.fork()
已于几天前添加到 v0.5 中。它简化了子进程的创建和通信 - 不完全是 Web Workers API,但也很接近,请参阅 URLhttps://github.com/joyent/node/blob/ master/doc/api/child_process.markdown。
JavaScript is single-threaded. That means that regardless of events, timeouts, or delaying with nextTick, any computation done will block the whole process.
If you split your processing in steps using
process.nextTick
, like it's done withsetTimeout(fn, 0)
on the client-side to avoid blocking the UI, you could spread your workload over a longer time span, giving some room for other functions to run.But that's a very innefective solution - the total amount of work is the same, distributed among all cycles (making each request a little slower). In practice, any kind of computation that is expected to take more than a few milliseconds should be offloaded to a different process. To maximize concurrency you should always return to the event loop as quickly as possible.
child_process.fork()
was added to v0.5 a few days ago. It simplifies child process creation and communication - not quite the web workers API, but close, see the URLhttps://github.com/joyent/node/blob/master/doc/api/child_process.markdown.
JavaScript 中没有真正的多线程,这就是使调用非阻塞所需要的。我唯一能想到的是网络工作者: https://developer.mozilla.org/en/Using_web_workers
There's no real multi-threading in JavaScript and that's what you need to make the call non-blocking. The only thing I can think of are web-workers: https://developer.mozilla.org/en/Using_web_workers