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.
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
.)
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
.)
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.
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.
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.
The feed for an IsolateStream
.
Any message written to this
is delivered to its respective IsolateStream
. IsolateSink
s are created by MessageBox
es.
IsolateSink
s can be transmitted to other isolates.
Tests whether other
is an IsolateSink
feeding into the same IsolateStream
as this one.
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 IsolateSink
s, 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.
Create an async error.
Closing multiple times is allowed.
Returns a string representation of this object.
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.
Closes the stream from the receiving end.
Closing an already closed port has no effect.
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.
Wraps unhandled exceptions thrown during isolate execution. It is used to show both the error message and the stack trace for unhandled exceptions.
Message being handled when exception occurred.
Wrapped exception.
Trace for the wrapped exception.
Returns a string representation of this object.
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.
Together with SendPort
, the only means of communication between isolates.
ReceivePort
s 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 SendPort
s.
Opens a long-lived port for receiving messages. The returned port must be explicitly closed through ReceivePort.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.
Sets up a callback function for receiving pending or future messages on this receive port.
Creates a new send port that sends to this receive port. It is legal to create several SendPort
s from the same ReceivePort
.
Together with ReceivePort
, the only means of communication between isolates.
SendPort
s are created from ReceivePort
s. Any message sent through a SendPort
is delivered to its respective ReceivePort
. There might be many SendPort
s for the same ReceivePort
.
SendPort
s can be transmitted to other isolates.
Returns an immutable hash code for this send port that is consistent with the == operator.
Tests whether other
is a SendPort
pointing to the same ReceivePort
as this one.
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.
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.
SendPortSync
s are created from ReceivePortSync
s. Any message sent through a SendPortSync
is delivered to its respective ReceivePortSync
. There might be many SendPortSync
s for the same ReceivePortSync
.
SendPortSync
s can be transmitted to other isolates.
Returns an immutable hash code for this send port that is consistent with the == operator.
Tests whether other
is a SendPortSync
pointing to the same ReceivePortSync
as this one.
Sends a synchronous message to this send port and returns the result.