Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process large number of tasks using Isolate

I have ~10k long-running tasks that I need to compute sequentially. I decided to use Isolates for this purpose. Question is should I create spawn Isolate every time for each individual task or Should I create only one Isolate for all the tasks' execution. I do not know how expensive is to create Isolates.

Source to create one Isolate and use it for all tasks:

import 'dart:isolate';

class DataPacket {
  SendPort port;
  int result;
}

class SquareRootCalculator {

  final ReceivePort _masterPort = new ReceivePort();
  SendPort _workerPort;

  SquareRootCalculator() {
    Isolate.spawn(isolateFunction, _masterPort.sendPort).then((isolate) {
      _masterPort.listen((data) {
         if (_workerPort == null)
           _workerPort = data.port;
        else {
          print(data.toString());
        }
      });
    });
  }

  input(int n) {
    _workerPort.send(n);
  }
}

void isolateFunction(SendPort masterPort) {
  ReceivePort _workerPort = new ReceivePort();

  DataPacket packet = new DataPacket();
  packet.port = _workerPort.sendPort;
  packet.result = -1;
  masterPort.send(packet);

  _workerPort.listen((data) {
    int out = calculate(data);

    DataPacket packet = new DataPacket();
    packet.port = _workerPort.sendPort;
    packet.result = out;
    masterPort.send(packet);
  });
}

int calculate(int number) {
  for (var i = 0; i < 1000000000; ++i) {} // long running task
  return number * number;
}

I can push the tasks

 SquareRootCalculator _calc = new SquareRootCalculator();

 for(int i = 0; i < 100; ++i){
    _calc.input(i); //task
    sleep(const Duration(seconds:1));
  }
like image 530
Anil8753 Avatar asked Jan 29 '23 02:01

Anil8753


1 Answers

Creating isolates is rather expensive if you create a lot.

I'd definitely suggest to only create one and push one task after the other using SendPort/ReceivePort.

https://pub.dartlang.org/packages/pool can help if you want a fixed number of isolates (lower than the number of tasks) run in parallel.

like image 74
Günter Zöchbauer Avatar answered Feb 07 '23 19:02

Günter Zöchbauer