start method

Future start()

Start the pool

Implementation

Future start() async {
  print('Creating a pool of $numberOfIsolates running isolates');

  _isolatesStarted = 0;
  _avgMicroseconds = 0;

  var last = Completer();
  for (var i = 0; i < numberOfIsolates; i++) {
    _isolateBusyWithJob.add(false);
    _isolateSendPorts.add(null);
  }

  var spawnSw = Stopwatch();
  spawnSw.start();

  for (var i = 0; i < numberOfIsolates; i++) {
    var receivePort = ReceivePort();
    receivePorts.add(receivePort);
    var sw = Stopwatch();

    sw.start();
    var params = _PooledIsolateParams(receivePort.sendPort, i, sw);

    var isolate = await Isolate.spawn<_PooledIsolateParams>(
        _pooledIsolateBody, params,
        errorsAreFatal: false);

    _isolates.add(isolate);

    receivePort.listen((data) {
      if (_state == IsolatePoolState.stoped) {
        print('Received isolate message when pool is already stopped');
        return;
      }
      if (data is _CreationResponse) {
        _processCreationResponse(data);
      } else if (data is _Request) {
        _processRequest(data);
      } else if (data is _Response) {
        _processResponse(data, _requestCompleters);
      } else if (data is _PooledIsolateParams) {
        _processIsolateStartResult(data, last);
      } else if (data is _PooledJobResult) {
        _processJobResult(data);
      }
    });
  }

  spawnSw.stop();

  print('spawn() called on $numberOfIsolates isolates'
      '(${spawnSw.elapsedMicroseconds} microseconds)');

  return last.future;
}