3

there is similar question (how to process a HTTP stream with Dart) about processing streams in dart2js. I am focused on vm.

I can read from spec that:

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 spawn), 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.

I ve learnt I cannot send to isolates and back following objects: HttpRequest and HttpResponse objects, I cannot send streams.

Q: I cannot really understand how should I process big chunk of data in isolate and then send it back to main isolate and in turn it can be send back to client.

Normally if I want to read a file. I can obtain a stream, apply transforms and then pipe a stream to http response. what is best practice to do that using isolates?

thanks for help!

4

1 回答 1

3

我提供了一个示例,但我将简要概述如何实现这一目标,尽管它可能不一定是最佳实践——它只是我个人知道的唯一方法。

我们有一个方法叫做Isolate.spawnUri(Uri uri, List<String> args, dynamic message)

message参数可以保存您的第一篇文章中提到的任何值。我们要做的是在主线程中创建一个新的ReceivePort并监听传入的数据,然后我们想要生成一个以messageReceivePorts 为对象的隔离.sendPort

然后隔离应该创建自己的ReceivePort并使用消息值发回自己的 sendPort 并侦听其接收端口。这实质上是创建了一种双向通信。允许我们保持隔离状态并来回发送工作。

来自隔离区的任何响应都将通过您的原始接收端口发送,并且您要发送到隔离区的任何数据都将通过隔离区刚刚发回的发送端口。

然后,您可以直接将流数据发送到隔离器以供其随意处理,并且它可以按原样发回数据(或者您可以在隔离器发送响应后关闭端口 - 由您决定如何进行它)。

我在 GitHub 上提供了一个示例: https ://github.com/TomCaserta/ExampleIsolate

请注意,如果您希望您的隔离在 dart2js 中工作,请务必通过以下命令在每个隔离文件上运行 dart2js:

dart2js -o <filename>.dart.js <filename>.dart

于 2014-02-27T20:52:56.240 回答