admin管理员组

文章数量:1291172

I read that the javascript language has characteristics that assist in the implementation of non-blocking IO which contributes to the success of projects like node.js. My question is what are these characteristics and why is non-blocking IO trickier to implement in other languages?

I read that the javascript language has characteristics that assist in the implementation of non-blocking IO which contributes to the success of projects like node.js. My question is what are these characteristics and why is non-blocking IO trickier to implement in other languages?

Share Improve this question edited Apr 9, 2012 at 18:03 rogermushroom asked Apr 9, 2012 at 16:33 rogermushroomrogermushroom 5,5864 gold badges45 silver badges69 bronze badges 6
  • 1 How wrote that? The asynchronous I/O is, as far as I know, a node-specific extension (though it does use a pure JS interface). – user395760 Commented Apr 9, 2012 at 16:35
  • 1 The language doesn't provide non-blocking IO. Certain frameworks do, however. – Cameron Commented Apr 9, 2012 at 16:36
  • Can you provide the document you read? Maybe they meant language syntax and semantics provides for non-blocking IO. – Andrew T Finnell Commented Apr 9, 2012 at 16:37
  • 2 @Cameron - I slightly disagree, some languages will aid you in implementing non-blocking IO while others will fight you at every turn. – ChaosPandion Commented Apr 9, 2012 at 16:39
  • 3 @ChaosPandion: Very true! But there's no mention of async I/O in the JS spec, as far as I know. – Cameron Commented Apr 9, 2012 at 16:43
 |  Show 1 more ment

4 Answers 4

Reset to default 6

JavaScript itself does not provide non-blocking IO. The underlying system calls that node.js uses do the non-blocking IO. JavaScript's first-class functions mean that it is easy to pass callbacks around when IO has pleted.

Other languages can do non-blocking IO just fine. node.js just argues that callbacks make it super-easy to reason about and handle non-blocking operations.

Ruby has EventMachine, which passes blocks around instead of functions. C can do non-blocking IO with function pointers, but then you don't get closures, so it is a bit more of a pain.

The reason that javascript is sometimes labeled as a non-blocking IO is because of the concept of anonymously defined, (event based), functions. Node.js specifically labels this as their reasoning why javascript is a good server side language. This however, is only a half truth, as it is not technically non-blocking, but it will continue to execute code while waiting for a callback from an anonymous callback/ajax function. I'm not sure if this is what you read, but an explanation offered in one Node tutorial is:

"The other method, the one taken by Node and some extremely fast modern servers such as Nginx and Thin, is to use a single non-blocking thread with an event loop. This is where the decision to use JavaScript really shines, since JavaScript was designed to be used in a single threaded event loop-based environment: the browser. JavaScript’s ability to pass around closures makes event-based programming dead simple. You basically just call a function to perform some type of I/O and pass it a callback function and JavaScript automatically creates a closure, making sure that the correct state is preserved even after the calling function has long since gone out of scope."

source: http://net.tutsplus./tutorials/javascript-ajax/this-time-youll-learn-node-js/

In reference to your multithreading tag, Node.js and Javascript are NOT multithreaded, they use a system of closures to preserve state while waiting for a callback. Therefore, they are NOT pletely non-blocking. There are plenty of scenarios where blocking can occur, but for most small implementations, a developer will never encounter a blocking situation.

see here for possible info on why node.js is bad: http://teddziuba./2011/10/node-js-is-cancer.html (Link broken)

and here for a rebuttle: http://rhyolight.posterous./nodejs-is-not-cancer (Link broken)

Asynchronous functions are usually event-based in JavaScript, which means registering callback-handlers. Your code runs on after the registration, but does not wait for the event - everything to be done after a event must be invoked from the handler. I hope that says all.

Of course there are exceptions, like window.alert / confirm / prompt in browsers.

https://youtu.be/dFnkZ15-_0o?t=2125 This excerpt from Andrew Mead's node.js course does a great job of visually explaining the differences between non-blocking and blocking I/O operations in JS. The clip is from 35:25 - 47:16.

本文标签: nonblockingHow does non blocking IO work in javascriptStack Overflow