3

是的,好人,我正在经历一种奇怪的行为

异步使用isolate的顶层函数时;

你可以在这里找到示例代码,但总之

作为隔离的顶级功能,这有效:

String _syncHandle(int data) {
  return 'done';
}

这不会:

Future<String> _syncHandle(int data) async {
  return 'done';
}

谁能解释我为什么?

(或者如果应该工作,为什么不在我的代码中这样做?)

先感谢您

弗朗切斯科

...

[编辑:刚刚注意到有人提出了类似的问题,

尽管如此,它仍然没有得到答复 从 Isolate 函数调用异步函数

加上问题在github上打开]

4

1 回答 1

0

忘了更新这个:/如果您查看问题中链接的代码

隔离日志/lib/provider/test_isolate.dart

  Future<void> _handle(int _m) async {
    final response = ReceivePort();
    isolateTest = await Isolate.spawn(_isolate, response.sendPort);
    final sendPort = await response.first as SendPort;
    final answer = ReceivePort();
    sendPort.send([_m, answer.sendPort]);
    await answer.first.then((p) { 
      _outbound.sink.add(p);});
  }

  static void _isolate(SendPort _initialReplyTo) {
    final port =  ReceivePort();
    _initialReplyTo.send(port.sendPort);
    port.listen((message) {
      final data = message[0] as int;
      final send = message[1] as SendPort;
      send.send(_syncHandle(data));
    });
  }
}

Future<String> _syncHandle(int data) async {
  return 'done';
}

注意 send.send(_syncHandle(data)); 部分

如果你这样做,你只能发送原语而不是期货,基本上就是这样

于 2019-10-01T21:40:28.627 回答