-
-
Save DmitrySoshnikov/1274980 to your computer and use it in GitHub Desktop.
class Printer extends Isolate { | |
main() { | |
port.receive((message, replyTo) { | |
if (message == null) port.close(); | |
else print(message); | |
}); | |
} | |
} | |
main() { | |
// first isolate process | |
new Printer().spawn().then((port) { | |
for (var message in ['Hello', 'from', 'other', 'isolate']) { | |
port.send(message); | |
} | |
port.send(null); | |
}); | |
// second isolate process | |
new Printer().spawn().then((port) { | |
for (var message in ['*Hello', '*from', '*other', '*isolate']) { | |
port.send(message); | |
} | |
port.send(null); | |
}); | |
} | |
// Results (WTF?) | |
Hello | |
from | |
other | |
isolate | |
*Hello | |
*from | |
*other | |
*isolate | |
// Why the output is sequential but not parallel? How the Dart's processes' scheduler work? |
FreakTheMighty, right, it's an anonymous callback function.
kaisellgren, yes, the same I thought (after programming in Erlang and having implemented similar scheduled processes in JS with yield
-- https://gist.github.com/1127536). However, after analyzing the source code of Dart's runtime library, I figured out that it's a design decision caused by impossibility of implementation of such parallel processes with using just setTimeout
scheduling (you may check btw, my similar setTimeout
processes -- https://gist.github.com/1103751). That is, callback process function should run to completeness and can't be interrupted in the middle. And exactly this cases sequential execution, but not parallel.
Recently I discussed the topic in Dart's mailing list: http://bit.ly/n3IUiT
@DmitrySoshnikov: as far as I know, most ES engines do not support yield
, except *Monkey. Therefore, I also believe the Dart engineers decided it is not possible to support "parallel processes" in transcompiled JS and this is the outcome :(. By the way, I like https://gist.github.com/1127536, did something similar once as well :)
This makes no sense.
spawn
indicates there's an internal scheduler involved, which would make me believe the loops run in parallel causing results not to be in a sequence.