飞镖分离株工人(Dart Isolates As Workers)

2019-07-29 03:40发布

编辑来让这个问题更加清晰。

我想用飞镖株(或Web工人)工作。 唯一的方法,我可以找到与主之间的交流隔离线程发送和从主线程中调用&然后 。 但是,这对于主线程的一些数据传递到分离的好方法。

什么是如果我想的分离是谁产生的信息吗? 就像一个游戏引擎,做一个工人的所有物理然后发送更新世界的信息主线程? 在JavaScript中,你可以在任何时候发送数据。 是否有飞镖的有效途径? 或者我还是要等待主线程给我打电话,然后把它传递给它?

PS我不知道,不叫&然后阻塞线程,直到回复做或不该?

Answer 1:

警告:此代码仅适用于达特的很老的版本。 它不会对飞镖1.0或更高版本。

至于你提到的将邮件投递到一个隔离,你需要有一个手柄上它的发送端口。

#import('dart:isolate');

main() {
  SendPort sendPort = spawnFunction(doWork);
  sendPort.call("hey 1").then((String res) => print("result was: [$res]"));
  sendPort.call("hey 2").then((String res) => print("result was: [$res]"));
}

doWork() {
  port.receive((msg, reply) {
    msg = "msg $msg";
    reply.send(msg);
  });
}

然而,由于镖主线程本身就是一种隔离您可以通过使用全球港口功能将数据发送给它:

#import('dart:isolate');
#import('dart:io');

main() {
   port.receive((data, reply) {
       // in here you can access objects created in the main thread
       print("handle [${data['text']}] for index ${data['index']}");
   });

   SendPort workPort = spawnFunction(doWork);
   workPort.send("msg", port.toSendPort());
}

doWork() {
   port.receive((msg, reply) {
      int i = 0;
      new Timer.repeating(1000, (Timer timer) {
         i++;
         var data = {
            "text": "$msg $i",
            "index": i
         };
         print("sending $data");
         reply.send(data);
      });
   });
}

注意,有什么可以送来回菌株之间也不同目前在JS以及VM隔离行为在一定的限度。 目前的限制是很好的描述在这里 。



Answer 2:

从DART 1.0,你可以使用这样的分离:

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

void doStuff(SendPort sendPort) {
  print('hi from inside isolate');
  ReceivePort receivePort = new ReceivePort();
  sendPort.send(receivePort.sendPort);

  receivePort.listen((msg) {
    print('Received in isolate: [$msg]');
    sendPort.send('ECHO: $msg');
  });

}

void main() {
  SendPort sendPort;

  ReceivePort receive = new ReceivePort();
  receive.listen((msg) {
    if (sendPort == null) {
      sendPort = msg;
    } else {
      print('From isolate: $msg');
    }
  });

  int counter = 0;

  Isolate.spawn(doStuff, receive.sendPort).then((isolate) {
    new Timer.periodic(const Duration(seconds:1), (t) {
      sendPort.send('Count is ${counter++}');
    });
  });
}


Answer 3:

下面是一个例子,其中母体创建两个分离物,然后两种分离物也相互交谈与父处理一起。

父代码:

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

main() {
  querySelector('#output').text = 'Your Dart app is running.';
  int counter = 0;

  // Parent - Child 1
  SendPort csendPort1;
  ReceivePort receivePort1 = new ReceivePort();
  // Parent - Child 2
  SendPort csendPort2;
  ReceivePort receivePort2 = new ReceivePort();
  // Child1 - Child2
  SendPort csendPort11;
  SendPort csendPort12;

  // Child 1
  receivePort1.listen((msg) {
    if (csendPort1 == null) {
      csendPort1 = msg;
    } else if (csendPort11 == null) {
      csendPort11 = msg;
    } else {
      print('$msg');`enter code here`
    }
  });

  bool child1 = false;
  Isolate.spawnUri(Uri.parse('child.dart'), [], receivePort1.sendPort).then((isolate) {
    print('Child 1 isolate spawned');
    new Timer.periodic(const Duration(milliseconds: 500), (t) {
      if (csendPort11 != null && csendPort12 != null && child1 == false) {
        child1 = true;
        csendPort12.send(csendPort11);
      } else {
        csendPort1.send('Parent-Child1: ${counter++}');
      }
    });
  });

  // Child 2
  receivePort2.listen((msg) {
    if (csendPort2 == null) {
      csendPort2 = msg;
    } else if (csendPort12 == null) {
      csendPort12 = msg;
    } else {
      print('$msg');
    }
  });

  bool child2 = false;
  Isolate.spawnUri(Uri.parse('child.dart'), [], receivePort2.sendPort).then((isolate) {
    print('Child 2 isolate spawned');
    new Timer.periodic(const Duration(milliseconds: 500), (t) {
      if (csendPort11 != null && csendPort12 != null && child2 == false) {
        child2 = true;
        csendPort11.send(csendPort12);
      } else {
        csendPort2.send('Parent-Child2: ${counter++}');
      }
    });
  });
}

儿童代码:

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

int pcounter = 0;
int ccounter = 0;

SendPort csendPort;
void handleTimeout() {
  csendPort.send("${ccounter++}");
}

main(List<String> args, SendPort psendPort) {
  // Parent Comm
  ReceivePort creceivePort1 = new ReceivePort();
  psendPort.send(creceivePort1.sendPort);

  creceivePort1.listen((msg) {
    psendPort.send('Child-Parent: ${pcounter++} - ${msg}');
  });

  // Child-Child Comm
  ReceivePort creceivePort2 = new ReceivePort();
  psendPort.send(creceivePort2.sendPort);

  creceivePort2.listen((msg) {
    if (csendPort == null) {
      csendPort = msg;
      csendPort.send("${ccounter++}");
    } else {
      print("Child-Child: $msg");
      var duration = const Duration(milliseconds: 2000);
      new Timer(duration, handleTimeout);
    }
  });
}

HTML代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="scaffolded-by" content="https://github.com/google/stagehand">
    <title>WebIsolateTest</title>
    <link rel="stylesheet" href="styles.css">
    <script defer src="main.dart" type="application/dart"></script>
    <script defer src="packages/browser/dart.js"></script>
</head>

<body>

  <div id="output"></div>

</body>
</html>


Answer 4:

您现在可以使用的MessageBox类的其他方式沟通。 将该代码作为立即发送从隔离码的消息,因为它接收在MessageBox的宿端。 主线程接收来自分离发送的消息并打印Dartium的控制台上。 一旦你收到的水槽可以启动你的游戏逻辑和使用发送接收的接收器对象的更新。

import 'dart:html';
import 'dart:isolate';

void main() {
  IsolateSink isolateSink = streamSpawnFunction(myIsolateEntryPoint);
  MessageBox isolateMessageBox = new MessageBox();
  isolateSink.add(isolateMessageBox.sink);
  isolateMessageBox.stream.listen((String data) {
    print(data);
  });
}

void myIsolateEntryPoint() {
  stream.listen((IsolateSink messageBoxSink) {
    messageBoxSink.add("Test");
  });
}


文章来源: Dart Isolates As Workers