Re:Unravelling `Async for` Loops
by speedgoose on Wednesday September 15, 2021 @03:41AM
(#28536362)
NodeJS has more than one single thread, network IOs notably are on another thread. That's the beauty of it.
Re:Unravelling `Async for` Loops
by patrickthebold on Wednesday September 15, 2021 @07:36AM
(#28537794)
Why would network IO be on another thread? libuv gives node non blocking network IO (or so was my understanding).
Unsure about network (if it's true that it's on a separate thread, perhaps it's because network operations take cpu time even when readied asynchronously--e.g. even after you select/[e]poll to determine what events to trigger next, the actual read/write/sendfile/whatever ops do take a little time). However, filesystem IO is performed in threads in libuv on many platforms, due to the unavailability of stable async IO capabilities.
There are plenty of languages with both async model and multicore runtime support. For example Kotlin on JVM. You still need to synchronize on shared resources (or use other patterns like message passing).
Async does not magically save you from synchronization. It's just a quirk of node.js implementation.