Dart隔离访问共享对象实例

时间:2014-08-22 18:06:05

标签: dart shared-memory dart-isolates

我目前正在使用隔离区将查询并行发送到数据库服务器。我有一个连接器对象来构建与数据库的连接,我想在所有隔离器中共享它,所以我不必为每个隔离器创建单独的连接。

到目前为止,我似乎只能在隔离区之间共享特殊的可序列化对象。我使用发送和接收端口进行消息传递。对于其他对象(例如我的connector-object),dart-vm会产生错误:

Illegal argument(s): Illegal argument in isolate message : (object extends NativeWrapper)

您知道在多个隔离区之间共享通用对象实例的任何方法吗?或者我是否必须为每个隔离区创建一个单独的实例?

谢谢!

佩德罗

1 个答案:

答案 0 :(得分:4)

来自SendPort.send方法代码doc

   * In the special circumstances when two isolates share the same code and are
   * running in the same process (e.g. isolates created via [Isolate.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.

我认为也不支持作为原生对象包装的对象 目前似乎唯一的方法是在每个隔离中创建一个新连接。

这些价​​值总是在工作

   * 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.