-1

I need some clarification on what exactly are the nodejs worker threads doing. I found contradicting info on this one. Some people say worker threads handle all IO, others say they handle only blocking posix requests (for which there is no async version).

For example, assume that I am not blocking the main loop myself with some unreasonable processing. I am just invoking functions from available modules and providing the callbacks. I can see that if this requires some blocking or computationally-expensive operation then it is handled to a worker thread. But for some async IO, is it initiated from the main libuv loop? Or is it passed to a worker thread, to be initiated from there?

Also, would a nodejs worker thread ever initiate a blocking (synchroneous) IO operation when the OS supports an async mode to do the same thing? Is it documented anywhere what kind of operations may end up blocking a worker thread for a longer time?

I'm asking this because there is a fixed-size worker pool and I want to avoid making mistakes with it. Thanks.

1 Answer 1

0

Network I/O on all platforms is done on the main thread. File I/O is a different story: on Windows it is done truly asynchronously and non-blocking, but on all other platforms synchronous file I/O operations are performed in a thread pool to be async and non-blocking.

4
  • So you mean on linux a synchroneous file IO request from my javascript ends up blocking a worker thread until completed (even though it could be done with libaio)? Then what about an async file IO that I issue from javascript?
    – tktuserA
    Jan 30, 2015 at 18:34
  • As far as I recall, it's not just as simple as plopping in libaio. libaio was investigated some time ago, but there were issues with missing or unreliable features and/or undesirable behavior. I'm not sure if the library's situation has improved since then or not. When you make an async file i/o request on *nix, it is being executed in the thread pool. When it finishes, it executes your callback and the thread that it previously used is now available for another file i/o request.
    – mscdex
    Jan 30, 2015 at 19:12
  • Thank you. Indeed, libaio had some problems in earlier linux versions. But lately where I work we tested it with massive amounts of real-time io and it seems to do just fine. So maybe the node people will reconsider at some point.
    – tktuserA
    Jan 30, 2015 at 19:55
  • If you are confident about it, you may consider creating an issue on github for node.js and/or io.js to see if there is interest in supporting it.
    – mscdex
    Jan 30, 2015 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.