4 ms·
Threads are not a menace if implemented correctly. Note how almost every evented system is written in a language with horrible threads support, e.g. Python. Tak
by thirsteh 13y ago
Threads are not a menace if implemented correctly. Note how almost every evented system is written in a language with horrible threads support, e.g. Python. Take a language like Go or Haskell, and threads become a highly performant (by virtue of transparently asynchronous I/O operations) means of writing code with many threads of control, and saves you from getting bundled up in callback spaghetti. (I love Twisted, but I really don't miss the callback spaghetti it inevitably causes. InlineCallbacks made it nicer, but it was still a pain.)
If you absolutely want to use JavaScript, then yes, you'll be writing event-driven code using a continuation passing style. JavaScript is probably one of the better languages for doing so, since the language is fundamentally single-threaded, and so virtually everything is asynchronous. The biggest argument for this is that you don't have to worry about synchronization. But please don't imply that event-driven programming is The One Way, or even the right way at all. If you want to scale, then you will eventually need to consider threading of some kind, whether it be the poor man's kind that Python and Node use, spawning multiple processes, or the good kind, fibers that automatically multiplex onto OS threads, as the Go and Haskell runtimes implement. Even the async poster-boy, nginx, does this via its workers.
- techaddict009 13y agoSaid it true.
- drorweiss 13y agoWhile it's true that threads are ok if implemented correctly, one needs to be smart to correctly work with threads, and still any concurrent code will be ~10x more expensive to write and maintain. In my perspective lack of threads is a blessing because it takes this complexity out of the equation. On the other hand, working with async callbacks also takes training and more careful thought of the bindings and closures. For me, after 6 months with node, I can say that I like it. It's superfast, ample modules to work with, and javascript is very convenient for working with data.
- thirsteh 13y agoYeah, like I said, running single-threaded, e.g. in Python with the GIL, or in JavaScript, saves you having to worry about synchronization. But that should be the only big argument for it, not performance and scalability.