But lack of threads has nothing to do with async. Node could still have async-everything and threads. That's the point I was trying to make - it's not the lack of threads that's nice, it's the nonblocking nature of everything.
Threads are still very relevant when it comes to working on cpu bound tasks in a way that needs access to shared data. Again, this isn't related to async or not. I'm not saying Java-style thread-based concurrency is better than node's. I'm saying there's a world were we could have both, and it would give us more flexibility.
Threads are still very relevant when it comes to working on cpu bound tasks in a way that needs access to shared data.
Right; if you need access to a large heap, cannot factor the data structure into shards, cannot defer to the OS via disk/disk cache, cannot isolate the access from the processing, and have minimal need for write mutexing, and for some strange reason cannot mount the data as SHM ; then maybe there is a case where direct access to heap RAM to perform cpu bound operations is the best option.
IME its very rare and the vast majority of times isolation of the critical sections which actually needed the heap and surface them over an api, or else splitting the problem into equal sized tasks over separate processes are better options.
Node could still have async-everything and threads.
I think the two are tied hand in hand; having access to thousand of libraries and vendor code where you automatically know there will be no threading and no blocking is exactly why you can count on async everything.
If threading was an option, over time people would elect to use it (for the wrong reasons) and it would start to proliferate through the ecosystem.
For what JS is, its better, systemically, that threads are not part of the language, imo.
Do you have an example problem in mind where it is the best solution?
Most problems in the space eclipse the space available in RAM anyway, so you end up back with multiple process. Its just a real strange corner case to need them, that I just havent found it. Even before working in JS, i had a flat no threading criteria for the team unless they could justify why it was the best case. and over 10 year, the only cases where workarounds for OS calls which blocked.
Threads are absolutely needed when you need to load and then parse large binary files. Loading can be done in parallel using the native calls but the parsing would block the main thread for a long time, or severely reduce responsiveness even if you split the parsing in multiple smaller batches.
Threads are vital for certain sets of responsive applications.
Threads are absolutely needed when you need to load and then parse large binary files.
Other that avoiding the kernel flaw around O_NONBLOCK on local drives, there is no need to thread read syscalls. Node conveniently has built in threads for those, so reading in the bytes works async out of the box.
split the parsing in multiple smaller batches.
If the parser yields often enough, it doesnt have to impact responsiveness at a. For example: replacing tight loops with Promise.each ensure granularity. if your file parse involves heavy CPU transformation of data beyond just reading into RAM, then you can pipeline that in via a separate process.
Threads are vital for certain sets of responsive applications.
Threads were vital to work around flawed OS's which cannot fork() well. If you are finding them vital today, I think there is a problem with the structure of your code.
I regularly deal with this issue, both with fork()s for heavy cpu transforms and async code for simple heap building, and I would consider a threaded approach to be a bug.
8
u/ryeguy Apr 23 '19
But lack of threads has nothing to do with async. Node could still have async-everything and threads. That's the point I was trying to make - it's not the lack of threads that's nice, it's the nonblocking nature of everything.
Threads are still very relevant when it comes to working on cpu bound tasks in a way that needs access to shared data. Again, this isn't related to async or not. I'm not saying Java-style thread-based concurrency is better than node's. I'm saying there's a world were we could have both, and it would give us more flexibility.