isolate_supervisor 1.0.1+2 isolate_supervisor: ^1.0.1+2 copied to clipboard
This library constructs higher-level isolate interfaces on top of the lower level dart:isolate library.
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 #
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;
}
Features and bugs #
Please file feature requests and bugs at the issue tracker.