Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate examples documentation #5332

Merged
merged 52 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ea6292b
updates page at /language/concurrency
ericwindmill Nov 8, 2023
984b8ac
adds ports sample code
ericwindmill Nov 8, 2023
64885b5
complete isolates page
ericwindmill Nov 8, 2023
a4533f9
fix code excerpts and unresolved links
ericwindmill Nov 9, 2023
39ff8f5
fix typos in image paths and links
ericwindmill Nov 9, 2023
5e7ce65
Merge branch 'main' into isolate-documentation
parlough Nov 10, 2023
360fed2
Fix broken/outdated link fragments
parlough Nov 10, 2023
5234ae5
Merge branch 'main' of https://github.com/dart-lang/site-www into iso…
ericwindmill Nov 14, 2023
5f63502
update doc regions
ericwindmill Nov 14, 2023
c396de2
Merge branch 'isolate-documentation' of https://github.com/dart-lang/…
ericwindmill Nov 14, 2023
de6a2e4
fix docregions
ericwindmill Nov 14, 2023
8b9b9e5
fix typo
ericwindmill Nov 14, 2023
239ba38
revert change to exceprt async-number-of-keys
ericwindmill Nov 14, 2023
9a1ad61
remove TODOs from code excerpt comments
ericwindmill Nov 14, 2023
55bd0b5
remove TODOs from code excerpt comments
ericwindmill Nov 14, 2023
465fe02
fix code excerpts
ericwindmill Nov 14, 2023
de17cde
code excerpts
ericwindmill Nov 14, 2023
af90e1f
Merge branch 'main' into isolate-documentation
ericwindmill Nov 14, 2023
213ea71
code excerpts
ericwindmill Nov 14, 2023
a698e3c
Merge branch 'main' into isolate-documentation
ericwindmill Nov 16, 2023
df22b47
update isolate example fib40
ericwindmill Nov 16, 2023
7ba1f69
Apply simple updates to concurrency.md page from @MaryaBelanger code …
ericwindmill Nov 27, 2023
0e33b26
address more concurrency.md code review comments from @maryabelanger
ericwindmill Nov 27, 2023
803052b
Merge branch 'main' of https://github.com/dart-lang/site-www into iso…
ericwindmill Dec 4, 2023
5af91d1
Apply suggestions from code @MaryaBelanger review
ericwindmill Dec 4, 2023
862aa10
address comments in concurrency.md
ericwindmill Dec 4, 2023
04e8c9d
Merge branch 'isolate-documentation' of https://github.com/dart-lang/…
ericwindmill Dec 4, 2023
673a143
update isolate_spawn_syntax.dart snippet
ericwindmill Dec 4, 2023
97557d3
update snippet to match
ericwindmill Dec 4, 2023
2b06e65
Don't split code excerpt instruction
parlough Dec 4, 2023
da359d7
Fix link definition
parlough Dec 4, 2023
8689f17
Fix fragment
parlough Dec 4, 2023
2ae0c5f
Merge branch 'main' into isolate-documentation
ericwindmill Dec 6, 2023
6a3c1ae
Apply suggestions from code review
ericwindmill Dec 7, 2023
6150027
checkin new examples
ericwindmill Dec 7, 2023
64d3d25
Merge branch 'isolate-documentation' of https://github.com/dart-lang/…
ericwindmill Dec 7, 2023
9a89093
apply changes from PR review
ericwindmill Dec 7, 2023
1757fc2
checkin
ericwindmill Dec 14, 2023
ed430da
Merge branch 'main' of https://github.com/dart-lang/site-www into iso…
ericwindmill Dec 14, 2023
43d552e
Merge branch 'main' of https://github.com/dart-lang/site-www into iso…
ericwindmill Jan 2, 2024
b18904a
add basic example
ericwindmill Jan 2, 2024
7fbb199
adds robust examples
ericwindmill Jan 2, 2024
6de7037
fix links
ericwindmill Jan 2, 2024
d30b659
fix lint checks in examples
ericwindmill Jan 2, 2024
4e846ac
update snippets
ericwindmill Jan 2, 2024
5bffa78
refresh exerpts
ericwindmill Jan 2, 2024
5860e2a
fix 404
ericwindmill Jan 2, 2024
7528f1f
fix alerts
ericwindmill Jan 2, 2024
a1c63a0
reference isolates page in concurrency page
ericwindmill Jan 3, 2024
a43eca4
Apply suggestions from code review
ericwindmill Jan 4, 2024
508c6ae
refresh snippets
ericwindmill Jan 4, 2024
8c31560
fix typo
ericwindmill Jan 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/concurrency/lib/basic_ports_example/complete.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

void main() async {
final worker = Worker();
await worker.spawn();
await worker.parseJson('{"key":"value"}');
}

// #docregion handleResponses parseJson
class Worker {
late SendPort _sendPort;
final Completer<void> _isolateReady = Completer.sync();
// #enddocregion handleResponses parseJson

// #docregion spawn
Future<void> spawn() async {
final receivePort = ReceivePort();
receivePort.listen(_handleResponsesFromIsolate);
await Isolate.spawn(_startRemoteIsolate, receivePort.sendPort);
}
// #enddocregion spawn

// #docregion handleResponses
void _handleResponsesFromIsolate(dynamic message) {
if (message is SendPort) {
_sendPort = message;
_isolateReady.complete();
} else if (message is Map<String, dynamic>) {
print(message);
}
}
// #enddocregion handleResponses

// #docregion startRemoteIsolate
static void _startRemoteIsolate(SendPort port) {
final receivePort = ReceivePort();
port.send(receivePort.sendPort);

receivePort.listen((dynamic message) async {
if (message is String) {
final transformed = jsonDecode(message);
port.send(transformed);
}
});
}
// #enddocregion startRemoteIsolate

// #docregion parseJson
Future<void> parseJson(String message) async {
await _isolateReady.future;
_sendPort.send(message);
}
// #enddocregion parseJson
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ignore_for_file: unused_field

import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

// #docregion
class Worker {
late SendPort _sendPort;

// spawn method

void _handleResponsesFromIsolate(dynamic message) {
if (message is SendPort) {
_sendPort = message;
} else if (message is Map<String, dynamic>) {
print(message);
}
}

// rest of class..
// #enddocregion

Future<void> spawn() async {
final receivePort = ReceivePort();
receivePort.listen(_handleResponsesFromIsolate);
await Isolate.spawn(_startRemoteIsolate, receivePort.sendPort);
}

static void _startRemoteIsolate(SendPort port) {
final receivePort = ReceivePort();
port.send(receivePort.sendPort);

receivePort.listen((dynamic message) async {
final decoded = jsonDecode(message as String);
port.send(decoded);
});
}

Future<void> parseJson(String message) async {
// TODO: Define a public method that can
// be used to send messages to the worker isolate.
}
}
44 changes: 44 additions & 0 deletions examples/concurrency/lib/basic_ports_example/parse_json.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ignore_for_file: unused_field

import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

// #docregion
class Worker {
late SendPort _sendPort;
final Completer<void> _isolateReady = Completer.sync(); // New

void _handleResponsesFromIsolate(dynamic message) {
if (message is SendPort) {
_sendPort = message;
_isolateReady.complete(); // New
} else if (message is Map<String, dynamic>) {
print(message);
}
}

// New
Future<void> parseJson(String message) async {
await _isolateReady.future;
_sendPort.send(message);
}
// rest of class..
// #enddocregion

Future<void> spawn() async {
final receivePort = ReceivePort();
receivePort.listen(_handleResponsesFromIsolate);
await Isolate.spawn(_startRemoteIsolate, receivePort.sendPort);
}

static void _startRemoteIsolate(SendPort port) {
final receivePort = ReceivePort();
port.send(receivePort.sendPort);

receivePort.listen((dynamic message) async {
final decoded = jsonDecode(message as String);
port.send(decoded);
});
}
}
27 changes: 27 additions & 0 deletions examples/concurrency/lib/basic_ports_example/spawn.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ignore_for_file: unused_field

import 'dart:async';
import 'dart:isolate';

class Worker {
// #docregion
Future<void> spawn() async {
final receivePort = ReceivePort();
receivePort.listen(_handleResponsesFromIsolate);
await Isolate.spawn(_startRemoteIsolate, receivePort.sendPort);
}
// #enddocregion

void _handleResponsesFromIsolate(dynamic message) {
// TODO: Define code that should be executed on the worker isolate.
}

static void _startRemoteIsolate(SendPort port) {
// TODO: Handle messages sent back from the worker isolate.
}

Future<void> parseJson(String message) async {
// TODO: Define a public method that can
// be used to send messages to the worker isolate.
}
}
23 changes: 23 additions & 0 deletions examples/concurrency/lib/basic_ports_example/start.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ignore_for_file: unused_field, unused_element
import 'dart:isolate';

// #docregion
class Worker {
Future<void> spawn() async {
// TODO: Add functionality to spawn a worker isolate.
}

void _handleResponsesFromIsolate(dynamic message) {
// TODO: Define code that should be executed on the worker isolate.
}

static void _startRemoteIsolate(SendPort port) {
// TODO: Handle messages sent back from the worker isolate.
}

Future<void> parseJson(String message) async {
// TODO: Define a public method that can
// be used to send messages to the worker isolate.
}
}
// #enddocregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ignore_for_file: unused_field

import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

class Worker {
Future<void> spawn() async {
final receivePort = ReceivePort();
receivePort.listen(_handleResponsesFromIsolate);
await Isolate.spawn(_startRemoteIsolate, receivePort.sendPort);
}

void _handleResponsesFromIsolate(dynamic message) {
// TODO: Define code that should be executed on the worker isolate.
}

// #docregion
static void _startRemoteIsolate(SendPort port) {
final receivePort = ReceivePort();
port.send(receivePort.sendPort);

receivePort.listen((dynamic message) async {
final decoded = jsonDecode(message as String);
port.send(decoded);
});
}
// #enddocregion

Future<void> parseJson(String message) async {
// TODO: Define a public method that can
// be used to send messages to the worker isolate.
}
}
109 changes: 109 additions & 0 deletions examples/concurrency/lib/robust_ports_example/complete.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

void main() async {
final worker = await Worker.spawn();
print(await worker.parseJson('{"key":"value"}'));
print(await worker.parseJson('"banana"'));
print(await worker.parseJson('[true, false, null, 1, "string"]'));
print(
await Future.wait([worker.parseJson('"yes"'), worker.parseJson('"no"')]));
worker.close();
}

// #docregion constructor
class Worker {
final SendPort _commands;
final ReceivePort _responses;
// #enddocregion constructor
final Map<int, Completer<Object?>> _activeRequests = {};
int _idCounter = 0;
bool _closed = false;

Future<Object?> parseJson(String message) async {
if (_closed) throw StateError('Closed');
final completer = Completer<Object?>.sync();
final id = _idCounter++;
_activeRequests[id] = completer;
_commands.send((id, message));
return await completer.future;
}

static Future<Worker> spawn() async {
// Create a receive port and add it's initial message handler
final initPort = RawReceivePort();
final connection = Completer<(ReceivePort, SendPort)>.sync();
initPort.handler = (initialMessage) {
final commandPort = initialMessage as SendPort;
connection.complete((
ReceivePort.fromRawReceivePort(initPort),
commandPort,
));
};

// Spawn the isolate
try {
await Isolate.spawn(_startRemoteIsolate, (initPort.sendPort));
} on Object {
initPort.close();
rethrow;
}

final (ReceivePort receivePort, SendPort sendPort) =
await connection.future;

return Worker._(receivePort, sendPort);
}

Worker._(this._responses, this._commands) {
_responses.listen(_handleResponsesFromIsolate);
}

void _handleResponsesFromIsolate(dynamic message) {
final (int id, Object? response) = message as (int, Object?);
final completer = _activeRequests.remove(id)!;

if (response is RemoteError) {
completer.completeError(response);
} else {
completer.complete(response);
}

if (_closed && _activeRequests.isEmpty) _responses.close();
}

static void _handleCommandsToIsolate(
ReceivePort receivePort,
SendPort sendPort,
) {
receivePort.listen((message) {
if (message == 'shutdown') {
receivePort.close();
return;
}
final (int id, String jsonText) = message as (int, String);
try {
final jsonData = jsonDecode(jsonText);
sendPort.send((id, jsonData));
} catch (e) {
sendPort.send((id, RemoteError(e.toString(), '')));
}
});
}

static void _startRemoteIsolate(SendPort sendPort) {
final receivePort = ReceivePort();
sendPort.send(receivePort.sendPort);
_handleCommandsToIsolate(receivePort, sendPort);
}

void close() {
if (!_closed) {
_closed = true;
_commands.send('shutdown');
if (_activeRequests.isEmpty) _responses.close();
print('--- port closed --- ');
}
}
}
48 changes: 48 additions & 0 deletions examples/concurrency/lib/robust_ports_example/spawn_1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ignore_for_file: unused_field, body_might_complete_normally_nullable, unused_element

import 'dart:async';
import 'dart:isolate';

// #docregion
class Worker {
final SendPort _commands;
final ReceivePort _responses;

static Future<Worker> spawn() async {
// Create a receive port and add it's initial message handler
final initPort = RawReceivePort();
final connection = Completer<(ReceivePort, SendPort)>.sync();
initPort.handler = (initialMessage) {
final commandPort = initialMessage as SendPort;
connection.complete((
ReceivePort.fromRawReceivePort(initPort),
commandPort,
));
};
// #enddocregion
throw UnimplementedError();
// #docregion
}
// #enddocregion

Future<Object?> parseJson(String message) async {
// TODO: ensure the port is still open
_commands.send(message);
}

Worker._(this._commands, this._responses) {
// TODO: initialize main isolate receive port listener
}

void _handleResponsesFromIsolate(dynamic message) {
// TODO: Handle messages sent back from the worker isolate.
}

static void _handleCommandsToIsolate(ReceivePort rp, SendPort sp) async {
// TODO: Handle messages sent back from the worker isolate.
}

static void _startRemoteIsolate(SendPort sp) {
// TODO: Initialize worker isolate's ports
}
}
Loading