I have this program that I'm using to learn about Dart's async programming.
import 'dart:io';
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
await for (final value in stream) {
print('consuming event $value');
sum = value;
}
return sum;
}
Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i ) {
sleep(const Duration(milliseconds: 400));
print('publishing event $i');
yield i;
}
}
Future<void> main() async {
var stream = countStream(10);
var sum = sumStream(stream);
print('working...');
sleep(const Duration(milliseconds: 500));
print('working...');
sleep(const Duration(milliseconds: 500));
print('working...');
sleep(const Duration(milliseconds: 500));
print(await sum); // 55
}
Output:
working...
working...
working...
publishing event 1
consuming event 1
publishing event 2
consuming event 2
publishing event 3
consuming event 3
publishing event 4
consuming event 4
publishing event 5
consuming event 5
publishing event 6
consuming event 6
publishing event 7
consuming event 7
publishing event 8
consuming event 8
publishing event 9
consuming event 9
publishing event 10
consuming event 10
55
In the code above, I chose not to await
for the result of sumStream()
immediately because I wanted to do some additional work while sumStream()
is busy consuming events from the provided stream. So my expectation was that sumStream()
would start running immediately while main()
is running. I expected the output to look something like below text. I expected the text working...
to be interleaved with the other print outs from the publisher countStream()
and the consumer sumStream()
.
working...
publishing event 1
consuming event 1
publishing event 2
consuming event 2
publishing event 3
consuming event 3
working...
publishing event 4
consuming event 4
publishing event 5
consuming event 5
publishing event 6
working...
consuming event 6
publishing event 7
consuming event 7
publishing event 8
consuming event 8
publishing event 9
consuming event 9
publishing event 10
consuming event 10
55
Is this because Dart is single threaded and thus it can't run main()
, countStream()
and sumStream()
at the same time?
If so, how could I change my program so that sumStream()
would run in parallel with main()
(make it multi-threaded)?
CodePudding user response:
I am pretty sure this is due to the sleep
function. Sleep function documentation:
Use this with care, as no asynchronous operations can be processed in a isolate while it is blocked in a sleep call.
I suggest trying this instead:
await Future.delayed(const Duration(milliseconds: 500));
Which won't block the processing of all the other async operations going on.