Also some little question about dart:io Process.Future<ProcessResult> Process.run(...) // Uses Fututre and is assyncProcess Process.start(..) // Don't uses Future and also is assync?They both asynchronous?
Thanks for the clarification on the second question. Everything is simple and clear.Future.wait(...) not block execution process.It run all futures in parallel and wait them all.I need realy blocking mechanism. Even the most primitive like that.while(true) {sleep(1000);if(done || isTimeToBreak()) {break;
How to blocking assync process execution?Or by other words how to convert async to sync?
Sorry, I meant the Timer interface from dart:isolate.
On Friday, 24 August 2012 01:23:31 UTC+10, Keith Schulze wrote:While its very rudimentary you could use the Timer interface from dart:io.For example:Future test = ///future from somewherenew Timer.repeating(500, (Timer t) {if (test.isComplete) {t.cancel();}});print(test.value);
#import("dart:io");
#import("dart:isolate");
void main() {
Completer<String> comp = new Completer<String>();
comp.complete(costlyWork());
Future test = comp.future;
new Timer.repeating(500, (Timer t) {
if(test.isComplete) {
t.cancel();
}
});
print("Hello, ${test.value}");
}
/**
* Dummy heavy function
*/
String costlyWork(){
var sw = new Stopwatch.start();
while (sw.elapsedInMs() < 5000){}
return "world";
}
Hi Bob,Thanks for clarifying that for me. I mocked up a quick example before posting last time that seemed to run okay. Given what you've just said, I'm not sure why it ran. So could you take a look and maybe explain it for me. I'm still coming to grips with the whole async model. Thanks#import("dart:io");
#import("dart:isolate");
void main() {
Completer<String> comp = new Completer<String>();
comp.complete(costlyWork());
Future test = comp.future;
new Timer.repeating(500, (Timer t) {
if(test.isComplete) {
t.cancel();
}
});
print("Hello, ${test.value}");
}
/**
* Dummy heavy function
*/
String costlyWork(){
var sw = new Stopwatch.start();
while (sw.elapsedInMs() < 5000){}
return "world";
}
Cheers,Keith
#import("dart:io");#import("dart:isolate");void main() {Completer<String> comp = new Completer<String>();comp.complete(costlyWork());Future test = comp.future;
print("Hello, ${test.value}");}/*** Dummy heavy function*/String costlyWork(){var sw = new Stopwatch.start();while (sw.elapsedInMs() < 5000){}return "world";}
On Friday, 24 August 2012 07:20:09 UTC+10, Bob Nystrom wrote:On Thu, Aug 23, 2012 at 8:57 AM, Keith Schulze <keith....@gmail.com> wrote:Sorry, I meant the Timer interface from dart:isolate.
On Friday, 24 August 2012 01:23:31 UTC+10, Keith Schulze wrote:While its very rudimentary you could use the Timer interface from dart:io.For example:Future test = ///future from somewherenew Timer.repeating(500, (Timer t) {if (test.isComplete) {t.cancel();}});print(test.value);That won't work, unfortunately. It will immediately throw a FutureNotCompleteException. That code will execute like so:1. Create the future.2. Create a new Timer, passing in the callback function. That function gets added to the list of callbacks that are waiting to be invoked on the event loop when ready.3. Try to print test.value. Since the future hasn't completed yet, it throws....4. If we hadn't thrown at some point in the future the timer fires and then the callback is invoked.In Dart, there is no way to block to turn an asynchronous operation into a synchronous one. Like JavaScript, the language's execution model simply doesn't allow it.Dart is built around a core event loop. Dart sits idle waiting for some internal operation to complete (like a Timer being ready, or an IO operation finishing). When that happens, it finds the callback associated with that event and invokes that Dart code. It then runs that Dart code to completion before it returns to the event loop to see if anything else is ready to run.That means you can't block to wait for an async operation to complete. Async operations only complete when all Dart code has finished running and control has returned to the event loop.If you sit and busy wait in your Dart code, the thing you're waiting on will never finish because it's waiting for your code to return so it can have its turn.This definitely does make some things painful to work with in Dart. You can't encapsulate an async operation. If you do something async, then everything above you on the callstack must also be written in async style.- bob
Ha, very true! Seems I have a lot to learn.... sorry for polluting the post everyone.
> I think you nailed it with your unintuitive comment. It definitely takes a while to get in the async mindset. If only Dart had an await keyword to make async programming feel like traditional code. :-)
>
> #import("dart:io");
> #import("dart:isolate");
>
> void main() {
> var result = await costlyWork();
> print("Hello, $result");
> }
Recently, I started to worry about this a little. There are people who actually think that this code runs synchronously! But, well, I can't think of anything better. Sometimes education is the correct solution.
LT
Thanks for the clarification on the second question. Everything is simple and clear.Future.wait(...) not block execution process.It run all futures in parallel and wait them all.I need realy blocking mechanism. Even the most primitive like that.
while(true) {sleep(1000);if(done || isTimeToBreak()) {break;
}}
I think that is a good thing. Obviously there are differences and developers should be aware of those differences, but the vast majority of the time await allows you to easily write code that looks like normal code and works exactly how you want it to work while still being async and providing the performance benefits of being async.JavaScript and Dart callback hell can get really ugly really fast. I think the await keyword alone would be a HUGE selling point for the Dart language over JavaScript.
On Thursday, August 23, 2012 11:01:29 PM UTC-5, Ladislav Thon wrote:
> I think you nailed it with your unintuitive comment. It definitely takes a while to get in the async mindset. If only Dart had an await keyword to make async programming feel like traditional code. :-)
>
> #import("dart:io");
> #import("dart:isolate");
>
> void main() {
> var result = await costlyWork();
> print("Hello, $result");
> }Recently, I started to worry about this a little. There are people who actually think that this code runs synchronously! But, well, I can't think of anything better. Sometimes education is the correct solution.
LT
--
> I think that is a good thing. Obviously there are differences and developers should be aware of those differences
Not really. There are fundamental differences in the computation model and developers _absolutely must_ know what code is sync and what is async. It wouldn't be that critical in a language without mutable state, but yeah...
But as I said, I believe that the correct solution here is proper education. Luckily, Dart isn't the first language to come with this, and AFAIK, Dart's await (in the prototype) had the same semantics as C#'s await. And I quite like that instead of an async keyword, functions in Dart still returned Future -- maybe it's a little more verbose, but I like that it's explicit and the main issue is the caller side anyway.
> but the vast majority of the time await allows you to easily write code that looks like normal code and works exactly how you want it to work while still being async and providing the performance benefits of being async.
>
> JavaScript and Dart callback hell can get really ugly really fast. I think the await keyword alone would be a HUGE selling point for the Dart language over JavaScript.
Totally agree with this. Await is a must and I truly believe it will come.
LT
It's difficult to have asyn code on the server. Since the request coming in from the client is going to return and if your asyn requests don't finish before then what can you do.
Yepp, await is a great idea. Has any progress been made here?
I sort of like the Go concurrency model of being synchronous by default and adding asynchronicity when needed on the higher levels…
Obviously this is not applicable for a single threaded event loop environment. But Dart does not have to be single threaded.
Being single-threaded is its own reward in many other ways. The most important one is to not have to worry about all the synchronization issues that comes with being multi-threaded: Race conditions, memory barriers/memory model, mutexes and the deadlocks they allow.
Since this keeps popping up in my inbox...
I'm with Crockford on this: http://www.yuiblog.com/blog/2010/08/30/yui-theater-douglas-crockford-crockford-on-javascript-scene-6-loopage-52-min/multi-threading shouldn't be in general purpose/application-level programming languages, either be blocking and enable synchronous code or non-blocking and be single-threaded and dispense with all multi-threading issues, having both is the worst of both worlds.
On the other hand, multithreading is THE low-level solution that is employed by OSes. Which makes it THE building block that you can build higher-level solutions on top of. That is -- you can implement actors on top of threads, you can implement STM on top of threads, you can implement data-flow concurrency on top of threads, etc.
IMO Dart's choice of single-threaded with concurrency via message-passing to isolates is the best choice, even without the constraints of single-threaded JS.
Yeah, that would be true if isolates were lightweight as they were promised to be. They are not, which makes them the worst of both worlds -- you can't use them as actors, and you can't use them as a primitive to build other concurrency abstractions.TBH, the more I think about it, the more I'm sure that Erlang did asynchrony/concurrency right 30 years ago, and since then, we are doing it all wrong. More at http://joearms.github.io/2013/04/02/Red-and-Green-Callbacks.htmlLT
That's correct.Process.run() returns a ProcessResultThis is basically the results of executing the process and nothing else. Just the exit code, and strings that were sent to stderr and stdout by the application.Process.start() returns a Process object. This object is then interacted with, async, by using call backs. This allows you to interactively communicate with a process. Such as wait for an output/prompt and send back data to the process while its running. You use callbacks such as onStart, and communicate with InputStream and OutputStream which you can also use callbacks to receive/send data such as 'onData'Basically Process.run() is a one shot, run a command and wait for the results. Like "ls -al"Process.start() is something you can interact with like say "telnet localhost", wait for the username prompt, then send "myUsername", wait for a password prompt and send your password... something you can interact with or call repeatedly.This really shows the benefits and uses of the two async methods in dart. Futures are great for one-shot deals. For asyncs that are called once and only once. Callbacks are a better fit when you need to deal with the async multiple times, or are reusable.I'm not sure what you mean by your first question, however, you can use Futures.wait() to wait for a list of futures, which will return a single Future with a List of the values.Hope this helps,Matt
On Thursday, August 23, 2012 9:09:11 AM UTC-3, mezoni wrote:Also some little question about dart:io Process.Future<ProcessResult> Process.run(...) // Uses Fututre and is assyncProcess Process.start(..) // Don't uses Future and also is assync?They both asynchronous?