dart.isolate


Top-Level Variables
stream: IsolateStream
Getters and Setters
port: ReceivePort
Functions
spawnFunction( topLevelFunction, unhandledExceptionCallback): SendPort
spawnUri(String uri): SendPort
streamSpawnFunction( topLevelFunction, unhandledExceptionCallback): IsolateSink
Classes
IsolateSink
IsolateSpawnException
IsolateStream
IsolateUnhandledException
MessageBox
ReceivePort
SendPort
SendPortSync

Concurrent programming using _isolates_: independent workers that are similar to threads but don't share memory, communicating only via messages.

See also: dart:isolate - Concurrency with Isolates(https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-dartisolate---concurrency-with-isolates) in the library tour.

Top-Level Variables

static final IsolateStream stream

The initial IsolateStream available by default for this isolate.

This IsolateStream is created automatically and is commonly used to establish the first communication between isolates. (See streamSpawnFunction.)

Getters and Setters

static ReceivePort get port

The initial ReceivePort available by default for this isolate.

This ReceivePort is created automatically and is commonly used to establish the first communication between isolates. (See spawnFunction and spawnUri.)

Functions

static SendPort spawnFunction( topLevelFunction, unhandledExceptionCallback)

Creates and spawns an isolate that shares the same code as the current isolate, but that starts from the specified function.

The topLevelFunction argument must be a static top-level function or a static method that takes no arguments. It is illegal to pass a function closure.

When any isolate starts (even the main script of the application), a default ReceivePort is created for it. This port is available from the top-level getter port defined in this library.

This function returns a SendPort derived from the child isolate's default port.

The optional unhandledExceptionCallback argument is invoked whenever an exception inside the isolate is unhandled. It can be seen as a big try/catch around everything that is executed inside the isolate. The callback should return true if it was able to handle the exception.

static SendPort spawnUri(String uri)

Creates and spawns an isolate that runs the code from the specified URI.

As with spawnFunction, the child isolate has a default ReceivePort, and this function returns a SendPort derived from it.

static IsolateSink streamSpawnFunction( topLevelFunction, unhandledExceptionCallback)

Creates and spawns an isolate that shares the same code as the current isolate, but that starts from the specified function.

The topLevelFunction argument must be a static top-level function or a static method that takes no arguments.

When any isolate starts (even the main script of the application), a default IsolateStream is created for it. This sink is available from the top-level getter stream defined in this library.

spawnFunction returns an IsolateSink feeding into the child isolate's default stream.

The optional unhandledExceptionCallback argument is invoked whenever an exception inside the isolate is unhandled. It can be seen as a big try/catch around everything that is executed inside the isolate. The callback should return true if it was able to handle the exception.


Abstract class IsolateSink extends EventSink<dynamic>

Constructors
IsolateSink()
Methods
==(dynamic other): bool
add(dynamic message): void
addError(dynamic errorEvent): void
close(): void

The feed for an IsolateStream.

Any message written to this is delivered to its respective IsolateStream. IsolateSinks are created by MessageBoxes.

IsolateSinks can be transmitted to other isolates.

Constructors

IsolateSink()

Methods

bool ==(dynamic other)

Tests whether other is an IsolateSink feeding into the same IsolateStream as this one.

void add(dynamic message)

Sends an asynchronous message to the linked IsolateStream; the message is copied to the receiving isolate.

The content of message can be: primitive values (null, num, bool, double, String), instances of IsolateSinks, and lists and maps whose elements are any of these. List and maps are also allowed to be cyclic.

In the special circumstances when two isolates share the same code and are running in the same process (e.g. isolates created via spawnFunction), it is also possible to send object instances (which would be copied in the process). This is currently only supported by the dartvm. For now, the dart2js compiler only supports the restricted messages described above.

void addError(dynamic errorEvent)

Create an async error.

void close()

Closing multiple times is allowed.


Class IsolateSpawnException implements Exception

Constructors
IsolateSpawnException(String _s)
Methods
toString(): String

Constructors

IsolateSpawnException(String _s)

Methods

String toString()

Returns a string representation of this object.


Class IsolateStream extends Stream<dynamic>

Methods
close(): void
listen( onData, onError, onDone, bool cancelOnError): StreamSubscription<dynamic>

Together with IsolateSink, the only means of communication between isolates.

Each IsolateStream has a corresponding IsolateSink. Any message written into that sink will be delivered to the stream and then dispatched to the stream's subscribers.

Methods

void close()

Closes the stream from the receiving end.

Closing an already closed port has no effect.

StreamSubscription<dynamic> listen( onData, onError, onDone, bool cancelOnError)

Adds a subscription to this stream.

On each data event from this stream, the subscriber's onData handler is called. If onData is null, nothing happens.

On errors from this stream, the onError handler is given a object describing the error.

If this stream closes, the onDone handler is called.

If cancelOnError is true, the subscription is ended when the first error is reported. The default is false.


Class IsolateUnhandledException implements Exception

Fields
message: dynamic
source: dynamic
stackTrace: Object
Constructors
IsolateUnhandledException(dynamic message, dynamic source, Object stackTrace)
Methods
toString(): String

Wraps unhandled exceptions thrown during isolate execution. It is used to show both the error message and the stack trace for unhandled exceptions.

Fields

final dynamic message

Message being handled when exception occurred.

final dynamic source

Wrapped exception.

final Object stackTrace

Trace for the wrapped exception.

Constructors

IsolateUnhandledException(dynamic message, dynamic source, Object stackTrace)

Methods

String toString()

Returns a string representation of this object.


Class MessageBox

Fields
sink: IsolateSink
stream: IsolateStream
Constructors
MessageBox()
MessageBox.oneShot()

The creator of the IsolateStream and IsolateSink that allow an isolate to exchange messages with other isolates.

Any message that is written into the sink (independent of the isolate) is sent to the stream where its subscribers can react to the messages.

Fields

final IsolateSink sink
final IsolateStream stream

Constructors

MessageBox()
MessageBox.oneShot()

Abstract class ReceivePort

Constructors
ReceivePort()
Methods
close(): void
receive( callback): void
toSendPort(): SendPort

Together with SendPort, the only means of communication between isolates.

ReceivePorts have a toSendPort method which returns a SendPort. Any message that is sent through this SendPort is delivered to the ReceivePort it has been created from. There, they are dispatched to the callback that has been registered on the receive port.

A ReceivePort may have many SendPorts.

Constructors

factory ReceivePort()

Opens a long-lived port for receiving messages. The returned port must be explicitly closed through ReceivePort.close.

Methods

void close()

Closes this receive port immediately. Pending messages will not be processed and it is impossible to re-open the port. Single-shot reply ports, such as those created through SendPort.call, are automatically closed when the reply has been received. Multiple invocations of close are allowed but ignored.

void receive( callback)

Sets up a callback function for receiving pending or future messages on this receive port.

SendPort toSendPort()

Creates a new send port that sends to this receive port. It is legal to create several SendPorts from the same ReceivePort.


Abstract class SendPort

Fields
hashCode: int
Getters and Setters
hashCode: int
Constructors
SendPort()
Methods
==(dynamic other): bool
call(dynamic message): Future<dynamic>
send(dynamic message, SendPort replyTo): void

Together with ReceivePort, the only means of communication between isolates.

SendPorts are created from ReceivePorts. Any message sent through a SendPort is delivered to its respective ReceivePort. There might be many SendPorts for the same ReceivePort.

SendPorts can be transmitted to other isolates.

Fields

final int hashCode

Getters and Setters

int get hashCode

Returns an immutable hash code for this send port that is consistent with the == operator.

Constructors

SendPort()

Methods

bool ==(dynamic other)

Tests whether other is a SendPort pointing to the same ReceivePort as this one.

Future<dynamic> call(dynamic message)

Sends a message to this send port and returns a Future of the reply. Basically, this internally creates a new receive port, sends a message to this send port with replyTo set to such receive port, and, when a reply is received, it closes the receive port and completes the returned future.

void send(dynamic message, SendPort replyTo)

Sends an asynchronous message to this send port. The message is copied to the receiving isolate. If specified, the replyTo port will be provided to the receiver to facilitate exchanging sequences of messages.

The content of message can be: primitive values (null, num, bool, double, String), instances of SendPort, and lists and maps whose elements are any of these. List and maps are also allowed to be cyclic.

In the special circumstances when two isolates share the same code and are running in the same process (e.g. isolates created via spawnFunction), it is also possible to send object instances (which would be copied in the process). This is currently only supported by the dartvm. For now, the dart2js compiler only supports the restricted messages described above.

Deprecation note: it is no longer valid to transmit a ReceivePort in a message. Previously they were translated to the corresponding send port before being transmitted.


Abstract class SendPortSync

Fields
hashCode: int
Getters and Setters
hashCode: int
Constructors
SendPortSync()
Methods
==(dynamic other): bool
callSync(dynamic message): dynamic

SendPortSyncs are created from ReceivePortSyncs. Any message sent through a SendPortSync is delivered to its respective ReceivePortSync. There might be many SendPortSyncs for the same ReceivePortSync.

SendPortSyncs can be transmitted to other isolates.

Fields

final int hashCode

Getters and Setters

int get hashCode

Returns an immutable hash code for this send port that is consistent with the == operator.

Constructors

SendPortSync()

Methods

bool ==(dynamic other)

Tests whether other is a SendPortSync pointing to the same ReceivePortSync as this one.

dynamic callSync(dynamic message)

Sends a synchronous message to this send port and returns the result.