4 ms·
> What we liked the most of Javascript was the fact that it’s an asynchronous language ‘by default’. That's not strictly true - a "while (true)" will lock up a
by davidw 14y ago
> What we liked the most of Javascript was the fact that it’s an asynchronous language ‘by default’.
That's not strictly true - a "while (true)" will lock up a Node process as far as I can tell. I think a more accurate way of stating it would be that "Javascript API's and libraries tend to be written with asynchronous use in mind", with lots of callbacks.
If you want something that's async at a deeper level, Erlang is worth a look.
- StavrosK 14y agoWill a "while True" not lock up an Erlang thread?
- davidw 14y agoNo: Erlang has an internal scheduler that makes sure that no process can lock the whole thing up. It's not impossible to wedge Erlang, of course, it's just not quite so easy.
- StavrosK 14y agoAh, so it uses preemptive rather than cooperative multitasking. I find that locking the whole thread is usually an error, so preemptive multitasking wouldn't help much there, and I like how cooperative multitasking lets me reason about the program, but I can see the benefits in both approaches.
- jerf 14y agoErlang does not have a problem reasoning about the program, because it doesn't share any data. They are actually independent from each other in the same ways that two OS processes are independent from each other (that is, there are still resource contention issues but abstract correctness don't unduly depend on each other). I'm running out of polite ways to say this, but Node advocates really need to learn about other ways of doing things before advocating so confidently that Node's way is better. Cooperative multithreading does not have the reasoning advantage, which is one of the reasons why it has been abandoned for so long at the OS level. It's incredibly harder to work with that preemptive multithreading sorts of things, combined with other techniques that have been developed over the decades. (Also, I said "Node advocates" and not StavrosK or "you" specifically; I mean that more generally than just your post here.)
- StavrosK 14y agoOh, I'm speaking from more of a Go/gevent approach. Obviously, a correctly-designed program would try to avoid shared data structures as much as possible, in any language, e.g. for Go you would write goroutines that communicate with channels. The "reasoning" I was referring to was more that I know that simple things like incrementing a global counter is guaranteed to be atomic. I agree with you that that's still a shared structure, and thus dirty, so I avoid it anyway. Now that you mention it, I don't actually know why I said that before, since I never write production code like that. I guess I was referring to ad-hoc scripts, where I like knowing that I don't need a lock to guarantee atomic operations, and thus they're a bit easier to write.
- exo762 14y agoThere are no reasons to use busy waiting in Erlang. Ever.
- StavrosK 14y agoThere are no reasons to use busy waiting anywhere. This isn't what we're discussing, though.
- scott_s 14y agoWell, yes, there is. When implementing locks, it depends on how likely and how long contention is. If the expected cost of busy waiting is less than the expected cost of interacting with a heavier-weight scheduling system (which may mean crossing the kernel boundary), then busy waiting is better. This is, of course, for lower level codes. For example, spin locks (so-called because they spin - or, busy wait - on a status variable) are frequently used in kernel code, and I have used them in a memory allocator.
- StavrosK 14y agoSure, in that case, it makes sense. As you say, though, it's for lower-level codes.
- davidw 14y agowhile(true) would be pretty bad form in a Node program too, and doesn't actually seem to be a big problem in practice for people.
- rdtsc 14y agoIt is a magnitude easier in Node to block the whole OS process by doing anything CPU intensive. In Erlang, if not using C-bindings, there is nothing a single Erlang process can do to block the whole runtime.
- matthavener 14y agoIs it even possible to busy wait in Erlang?
- oinksoft 14y agoIt will block your process (just like a `receive' does), but a good Erlang/OTP system is generally comprised of a great many processes. If one process can block, your design is wrong, and the system itself is designed to be robust when things block like this. You expect your processes to be blocked, but you design things so that truly concurrent activities are being run in parallel.
- rdtsc 14y agoNo. It is funny you'd ask though, I just read this post yesterday: How Erlang Does Scheduling : http://jlouisramblings.blogspot.dk/2013/01/how-erlang-does-scheduling.html http://jlouisramblings.blogspot.dk/2013/01/how-erlang-does-s...
- brown9-2 14y agoBeyond this, is there any part of the JavaScript language itself that is inherently asynchronous or enables the writing of asynchronous code? I believe the answer is no - the async nature is provided by V8 and the core Node libraries.
- archangel_one 14y agoI would say yes, it's very easy to write inline anonymous functions which are very very nice for a primarily callback-driven model. I much prefer Python as a language overall, but I think writing code for "Node.py" would be an awful lot harder.
- pindi 14y ago"Node.py" is Twisted [1] and/or Tornado [2]. And you're definitely correct. [1] http://twistedmatrix.com/trac/ http://twistedmatrix.com/trac/ [2] http://www.tornadoweb.org/ http://www.tornadoweb.org/
- xyzzy123 14y agoAgree, Python doesn't feel as natural for callback-style. I do like the gevent (http://www.gevent.org/ http://www.gevent.org/) or greenlet style code though.
- mason55 14y agoI disagree. Try writing fully async Java - it's much, much harder without functions as first-class objects and true closures.