How do I write non-blocking code in Node.js?

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]: [2]: [3]:
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前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准