initialize method

  1. @override
Future<void> initialize({
  1. int? isolatesCount,
  2. int tasksPerIsolate = defaultTasksPerIsolate,
  3. WorkerInitializer? initializer,
  4. String isolatesPrefix = defaultIsolatePrefix,
})
override

Initializes worker.

Worker manager can be lazily initialized on the first execution, so you can omit calling initialize.

To initialize worker with a custom amount of isolates, use the isolatesCount parameter. Default value is calculated by the following formula: max(1, (numberOfProcessors / 2).floor()) Please keep in mind that Flutter already uses 3 threads: Dart main, native main and GPU. So your isolatesCount should be less than numberOfProcessors - 3.

Each isolate can execute one or more tasks asynchronously (thanks to async IO and event loop). tasksPerIsolate parameter is used to set maximum number of tasks that one isolate can perform asynchronously.

initializer is a function that will be executed in the each worker isolate. It can be used to initialize something in the worker isolate.

isolatesPrefix will be used to set isolates debug name. Debug name will be visible in the debugger.

Implementation

@override
Future<void> initialize({
  int? isolatesCount,
  int tasksPerIsolate = defaultTasksPerIsolate,
  WorkerInitializer? initializer,
  String isolatesPrefix = defaultIsolatePrefix,
}) async {
  assert(
    isolatesCount == null || isolatesCount > 0,
    "`isolatesCount` must be greater than zero if specified.",
  );
  assert(
    !_isInitialized,
    "`CombineWorker` is already initialized.\n"
    "This may happen if you call some `execute` function before `initialize`.\n",
  );
  assert(
    tasksPerIsolate > 0,
    "`tasksPerIsolate` parameter must be greater that zero",
  );

  _isInitialized = true;
  _workerManager = effectiveWorkerFactory.create(
    tasksPerIsolate: tasksPerIsolate,
    isolatesCount: isolatesCount,
  );
  await _effectiveWorkerManager.initialize(
    initializer: initializer,
    isolatesPrefix: isolatesPrefix,
  );
}