Hacker News new | past | comments | ask | show | jobs | submit login

I'm still trying to understand how node actually works. I'm just about to start digging through its source code. It seems most people view node as a magical mystery that they don't understand why it works, just that it does. How is a single threaded app doing things in parallel? Is it like a game loop where it iterates through all its pending operations and gives each a slice of time to progress forward? Are deeper parts of node multi threaded? The callbacks being called serially makes perfect sense, its the parallelness of the actual operations that confuses me.




Yes, it's similar to a game loop, but in Node it's called an event loop. All JavaScript code executes in the main thread, with the event loop (libev) coordinating asynchronous IO APIs, the thread pool, and signal handlers. Node continues executing until there are no more pending operations, then exits.

Most IO in Node doesn't require additional threads; it uses libeio for asynchronous IO on POSIX systems (I believe they're currently working on an abstraction for asynchronous IO in POSIX and Windows).

It does need to use the thread pool for IO operations that only have a blocking API (stat() and friends, legacy database libraries, etc). The blocking API call is executed in a separate thread so the main thread is never blocked on IO.

This is a bit of a (necessary) hack, and ideally all IO could be done using non-blocking APIs.


I too still don't understand 100% what's going on behind the scenes of node/V8 regarding threading/parallelism, but this post here helped me a lot to understand it from a bird's eye perspective:

http://debuggable.com/posts/understanding-node-js:4bd98440-4...

It's also linked from The Node Beginner Book.


There are io threads just like there are for xhr in the browser -- but your app code is single threaded.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: