Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dart Isolate, Port and Capabilities

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Dart Isolate, Port and Capabilities

Dart's basic concurrent language construct Isolate and communication through ReceivePort/SendPort. and Capability based authentication way.

Avatar for UEHARA Junji

UEHARA Junji

July 18, 2018
Tweet

More Decks by UEHARA Junji

Other Decks in Programming

Transcript

  1. DartͰݟͳΕ͵΋ͷΛௐ΂Δ γϦʔζ 1. DartͷඇಉظετϦʔϜΛͪΐͬͱݟͯΈΔ →Dart Meetup Tokyo #4 (2018/3/29) 2.

    Dartͷม਺ఆٛ࣌ͷम০static/final/constɺͦͯ͠ const constructorʹ͍ͭͯ →Qiitaهࣄ(2018/6/27) 3. DartͷΞΠιϨʔτͱέΠύϏϦςΟɺPortʹ͍ͭ ͯ →Dart Meetup Tokyo #5 (2018/7/18) ←ࠓ͜͜
  2. (༨ஊ)ϒϥ΢β&Dart VMͰฒߦ ॲཧίʔυΛڞ༗͍ͨ͠ͱ͖ a.dart: 
 void test() { print("a"); }

    b.dart:
 void test() { print("b"); } default.dart:
 void test() { print("default"); } main.dart:
 import 'default.dart'
 if (a) 'a.dart'
 if (b) 'b.dart';
 main() {
 test();
 } Configuration Specific Import(Dart2?)Λ࢖͑Δ dart -Da=true main.dart
  3. ΞΠιϨʔτؒ௨৴: Port import ‘dart:isolate'; ReceivePort receivePort = new ReceivePort(); Isolate

    isolate = await Isolate.spawn(
 entryPoint,
 receivePort.sendPort,
 options); void entryPoint(sendPort) {
 sendPort.send(“hello”);
 };
  4. ReceivePort/SendPort ΞΠιϨʔτؒ௨৴Ͱ౉ͤΔͷ͸ ϓϦϛςΟϒ஋(null, num, bool, double, String) SendPort(extends Capability) ্هΛཁૉͱ͢ΔϦετɺϚοϓ

    + ্هΛϝϯόʔͱ͢ΔΫϥεͷΦϒδΣΫτ ※ ಉ͡Dart VMϓϩηε্Ͱಈ͍͍ͯΔ৔߹ݶఆ ҎԼ͸ૹ৴ෆՄ Ϋϩʔδϟ(ૹ৴࣌Τϥʔ) Future(ΞΠιϨʔτΛ·͕ͨͬͯcomplete͸Ͱ͖ͳ͍)
  5. ΞΠιϨʔτؒ௨৴: ਌ˠࢠ import ‘dart:isolate'; ReceivePort receivePort = new ReceivePort(); Isolate

    isolate = await Isolate.spawn(
 entryPoint,
 receivePort.sendPort,
 options); receivePort.listen((toChild){ toChild.send(“hello”)); void entryPoint(sendPort) {
 ReceivePort rp2 = new ReceivePort();
 sendPort.send(rp2.sendPort);
 };
  6. IsolateΫϥε ΞΠιϨʔτ࣮ߦ੍ޚͷͨΊͷϝιου܈ pause(), resume(), kill(), setErrorsFatal() ੍ޚϙʔτ SendPort get controlPort;

    έΠύϏϦςΟϓϩύςΟ Capability get pauseCapability; Capability get terminateCapability;
  7. TIPS: ΍͓͍ͬͯͨํ͕ྑ͍͜ͱ: ࢠΞΠιϨʔτଆͰͷΤϥʔදࣔ ReceivePort onErrorPort = new ReceivePort();
 onErrorPort.listen((error) {


    print("error: ${error}.");
 }); Isolate isolate = await Isolate.spawn(
 entryPoint,
 onError: onErrorPort.sendPort);
  8. Streamͱͯ͠ͷReceivePort ReceivePort.close()͠ͳ͍ݶΓແݶετϦʔϜ ͔͠͠IsolateଆͰReceivePort.close()͢Δ͜ͱ͸Ͱ͖ͳ͍ https://github.com/dart-lang/sdk/issues/28731 Isolateͷݺͼग़͠ଆReceivePortΛlistenͯࣗ͠ݾcloseͤ͞Δ? Ͱ΋listenͨ͠ΒStreamΛ࠶౓࢖༻Ͱ͖ͳ͍ ReceivePort͕ແݶετϦʔϜʹͳΔͱ for asyncͰ͖ͳ͍ StreamॲཧͷҰ෦͕ແݶϒϩοΫ͢Δ

    ༗ݶͰ͋Δ͜ͱΛલఏͱͨ͠ετϦʔϜࢼݧ(emitsInOrder)Ͱ͖ͳ͍ ReceivePort͕StreamͰ͋Δ͜ͱ͸ͦ΋ͦ΋ઃܭ্ଥ౰ͳͷ͔? We chose to make ReceivePort look like a Stream. That might have been a mistake, but it's too late to change now. StreamͰ͸ͳ͍RawReceivePort΋͋Δ