Home > database >  Spawning Isolates in for loop and processing parallelly in Dart
Spawning Isolates in for loop and processing parallelly in Dart

Time:03-23

for(int i=0;i<2;i  ){
   ..............preprocessing(pr)...........
   ..... = await someFunc(someData[i]) (this line spawns off new Isolate)
   ..............postprocessing(po)..........
   }

I am writing this kind of function in dart and what I expect is to get this order pr0 -->someFunc(data[0]) --> pr1 --> someFunc(data[1]) --> post processing of whichever (Isolate finishes first ). But I know that I cant get the same effect using for loop. It is giving me of the order pr0 --> someFunc(data[0]) --> pr1 --> someFunc(data[1]) --> po0 --> po1. How should I implement to get the above effect.

Thanks in advance.

CodePudding user response:

You could try using Future.wait instead (https://api.dart.dev/stable/2.16.1/dart-async/Future/wait.html).

final results = await Future.wait([
  for (int i = 0; i < 2; i  ) someFunc(someData[i]),
]);
  • Related