Dart 隔离对共享对象实例的访问

标签 dart shared-memory dart-isolates

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

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

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

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

谢谢!

佩德罗

最佳答案

来自 SendPort.send 方法代码文档

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

我假设作为 native 对象包装器的对象也不受支持。
目前看来唯一的方法是在每个隔离中创建一个新连接。

这些价​​值观始终有效

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

关于Dart 隔离对共享对象实例的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25453088/

相关文章:

dart - 在 Dart 中将项目排序到列表前面

android - Flutter SafeArea : avoid notches, 但忽略圆角

c++ - 有没有办法列出所有共享内存对象的名称?

flutter - 如何在 Isolate 上实例化本地通知?

json - 使用Redstone Mapper解码Dart中的List <int>

flutter - 在抖动中出现空错误时调用了PhotoURL

c - protected 共享内存?

c++ - 单个共享内存中的不同类型

Flutter:从 native 代码产生的隔离调用插件时出现 MissingPluginException

调试隔离