6 ms·
>read from multiple queues at the same time That's a trivial thing in javascript.
by moralestapia 5y ago
>read from multiple queues at the same time
That's a trivial thing in javascript.
- metadat 5y agoHow? Javascript is single threaded run loop.
- lalaithion 5y agoConcurrency is not parallelism: https://go.dev/blog/waza-talk#:~:text=In%20programming%2C%20concurrency%20is%20the,lots%20of%20things%20at%20once https://go.dev/blog/waza-talk#:~:text=In%20programming%2C%20....
- metadat 5y ago>read from multiple queues at the same time Agreed, and things happening "at the same time" are possible only with parallelism. Hence my question =)
- moralestapia 5y agoI interpreted "at the same time" as "without blocking", But sure you could also add parallelism using a few built-in Node modules (there's cluster and worker threads, also don't know if atomics are already in Node, they are in V8), It's not trivial anymore but also not that hard. A newcomer could figure it out in ~30 mins from the docs.
- metadat 5y agoGotcha, sounds like we interpreted it differently. I appreciate you taking the time to clarify, I am an old hand as JS but didn't know true parallelism had become feasible these days. Thank you, sincerely!
- moralestapia 5y agoDon't worry, JS has come a long way. During my PhD we had to crunch some numbers and my group usually makes use of C++ with libs for that. I tried doing it in Node using cluster and let it spread through our whole cluster of nodes (funny how it plays on words) and performance was worse obv. but not that bad, it was about 2x slower, but with that I was able to write code and test it in hours instead of days, and it didn't matter to me if a program that was going to take 2 hours was now taking 4 as I was going home anyway and check it out next morning ¯\_(ツ)_/¯. Plus, my experiment had a REST API for free (well, like 8 lines of code) and when I showed that to them they were :O.
- paskozdilar 5y agoIf you could spare some time, would you please demonstrate an example?
- vbezhenar 5y agoYou can use Promise.race to wait for the first resolved promise.
- moralestapia 5y agoAbusing notation a bit, var a = new Queue(); var b = new Queue(); a.read(() => some callback); b.read(() => some callback); And code continues here without blocking. To be honest, I think you could do the same in any (slightly) modern language.
- paskozdilar 5y agoIs the read() call running a task in the background? In that case, that's equivalent to launching two threads. For n queues, you'd need n tasks, and they'd hang in the background in case they never get a result and take up memory.