isolate_supervisor 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

Isolate Supervisor #

This library constructs higher-level isolate interfaces on top of dart:isolate library.

Usage #

To use this library, just create an instance of IsolateSupervisor like:

  final supervisor = IsolateSupervisor();

  //or you can also define how many isolates to spawn:
  final supervisor = IsolateSupervisor.spawn(count: 2, lazily: true));

As a task, we need to define our entry point function:

Any top-level function or static method is a valid entry point for an isolate.

  Future<int> entryPoint(IsolateContext context) async
  {
    int timeout = context.arguments.nearest();
    final duration = Duration(milliseconds: timeout);

    return await Future.delayed(duration, () => timeout);
  }

  // returns multiple values
  Stream<num> streamEntryPoint(IsolateContext context) async*
  {
    int timeout = context.arguments.nearest();
    final duration = Duration(milliseconds: timeout);

    yield timeout;
    yield await Future.delayed(duration, () => timeout * timeout);
  }

And execute your tasks:

  final result = await supervisor.compute(entryPoint, [42]);
  print(result);

  final results = supervisor.launch(streamEntryPoint, [42]);
  await for (final result in results) {
    print(result);
  }

IsolateContext Interface #

  • sink is the isolate output sink.

  • input is the isolate input stream.

  • lock is a method that returns the primitive lock object.

  • arguments is a arguments collection passed into the isolate.

  • isolateName is the name used to identify isolate in debuggers or loggers.

Locks #

At any time, a lock can be held by a single isolate, or by no isolate at all. If a isolate attempts to hold a lock that’s already held by some other isolate, execution of the first isolate is halted until the lock is released.

  Future<String> lockEntryPoint(IsolateContext context) async
  {
    final name = context.arguments[0];
    final lock = context.lock('sample');

    await lock.acquire(); // will block if lock is already held
      await Future.delayed(Duration(milliseconds: 100));
    lock.release();

    return name;
  }

Communication example #

  void main() async
  {
    final supervisor = IsolateSupervisor();

    final task = await supervisor.execute(communicationEntryPoint);
    await task.wait;

    task.send('Hello');
    print(await task.output.first);

    await task.done;
    await supervisor.dispose();
  }

  Stream<String> communicationEntryPoint(IsolateContext context) async*
  {
    String hello = await context.input.first;
    yield '${context.isolateName}: $hello World!';
  }

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.2 #

  • Added IsolateSupervisor.execute().
  • Documentation and Example Updates.

1.0.1+2 #

  • Documentation Updates.

1.0.1 #

  • Fixes bug where IsolateSupervisor.dispose() cannot correctly terminate isolates.
  • Deprecates IsolateSupervisor.restart(), use IsolateSupervisor.reset() instead.

1.0.0 #

  • Initial version.

example/isolate_supervisor_example.dart

import 'package:isolate_supervisor/isolate_supervisor.dart';

void main() async
{
  final supervisor = IsolateSupervisor();

  final result = await supervisor.compute(fibEntryPoint, [42]);
  print(result);

  final results = supervisor.launch(streamEntryPoint, [42]);
  await for (final result in results) {
    print(result);
  }

  await supervisor.dispose();
}

int fibEntryPoint(IsolateContext context)
{
  int n = context.arguments.nearest();

  int fib (int n)
  {
    if (n < 2) return n;
    return fib(n - 2) + fib(n - 1);
  };

  return fib(n);
}

Stream<num> streamEntryPoint(IsolateContext context) async*
{
  int timeout = context.arguments.nearest();
  final duration = Duration(milliseconds: timeout);

  yield timeout;
  yield await Future.delayed(duration, () => timeout * timeout);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  isolate_supervisor: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:isolate_supervisor/isolate_supervisor.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
44
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because:

  • package:isolate_supervisor/isolate_supervisor.dart that imports:
  • package:isolate_supervisor/src/supervisor.dart that imports:
  • package:isolate_supervisor/src/registry/mutex_registry.dart that imports:
  • package:isolate_supervisor/src/events/events.dart that imports:
  • dart:isolate

Package not compatible with runtime js

Because:

  • package:isolate_supervisor/isolate_supervisor.dart that imports:
  • package:isolate_supervisor/src/supervisor.dart that imports:
  • package:isolate_supervisor/src/registry/mutex_registry.dart that imports:
  • package:isolate_supervisor/src/events/events.dart that imports:
  • dart:isolate

Health suggestions

Format lib/isolate_supervisor.dart.

Run dartfmt to format lib/isolate_supervisor.dart.

Format lib/src/base/isolate_context.dart.

Run dartfmt to format lib/src/base/isolate_context.dart.

Format lib/src/base/isolate_event.dart.

Run dartfmt to format lib/src/base/isolate_event.dart.

Fix additional 24 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/base/isolate_executor.dart (Run dartfmt to format lib/src/base/isolate_executor.dart.)
  • lib/src/base/isolate_process.dart (Run dartfmt to format lib/src/base/isolate_process.dart.)
  • lib/src/base/isolate_registry.dart (Run dartfmt to format lib/src/base/isolate_registry.dart.)
  • lib/src/base/isolate_runnable.dart (Run dartfmt to format lib/src/base/isolate_runnable.dart.)
  • lib/src/base/isolate_task.dart (Run dartfmt to format lib/src/base/isolate_task.dart.)
  • lib/src/events/events.dart (Run dartfmt to format lib/src/events/events.dart.)
  • lib/src/events/lock_events.dart (Run dartfmt to format lib/src/events/lock_events.dart.)
  • lib/src/events/result_events.dart (Run dartfmt to format lib/src/events/result_events.dart.)
  • lib/src/events/value_events.dart (Run dartfmt to format lib/src/events/value_events.dart.)
  • lib/src/exceptions.dart (Run dartfmt to format lib/src/exceptions.dart.)
  • lib/src/helpers/hash.dart (Run dartfmt to format lib/src/helpers/hash.dart.)
  • lib/src/helpers/lock.dart (Run dartfmt to format lib/src/helpers/lock.dart.)
  • lib/src/isolate/context/context.dart (Run dartfmt to format lib/src/isolate/context/context.dart.)
  • lib/src/isolate/context/context_arguments.dart (Run dartfmt to format lib/src/isolate/context/context_arguments.dart.)
  • lib/src/isolate/context/context_lock.dart (Run dartfmt to format lib/src/isolate/context/context_lock.dart.)
  • lib/src/isolate/context/context_sink.dart (Run dartfmt to format lib/src/isolate/context/context_sink.dart.)
  • lib/src/isolate/isolate_process.dart (Run dartfmt to format lib/src/isolate/isolate_process.dart.)
  • lib/src/isolate/isolate_worker.dart (Run dartfmt to format lib/src/isolate/isolate_worker.dart.)
  • lib/src/isolate/isolate_wrapper.dart (Run dartfmt to format lib/src/isolate/isolate_wrapper.dart.)
  • lib/src/registry/mutex_registry.dart (Run dartfmt to format lib/src/registry/mutex_registry.dart.)
  • lib/src/registry/values_registry.dart (Run dartfmt to format lib/src/registry/values_registry.dart.)
  • lib/src/schedule/task.dart (Run dartfmt to format lib/src/schedule/task.dart.)
  • lib/src/schedule/task_action.dart (Run dartfmt to format lib/src/schedule/task_action.dart.)
  • lib/src/supervisor.dart (Run dartfmt to format lib/src/supervisor.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.9.0
test ^1.14.6