飞镖/颤振:隔离的顶级功能的异步行为 [英] Dart / Flutter: async behaviour of an Isolate's top level function

查看:71
本文介绍了飞镖/颤振:隔离的顶级功能的异步行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,好的人, 我遇到了奇怪的行为

Aye Aye good people, I'm experiencing a weird behavior

异步使用隔离的顶级功能时;

when using the top level function of an isolate asynchronously;

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

you can find example code HERE, but in short

作为隔离的顶级功能:

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

这不是:

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

有人能解释我为什么吗?

can anybody explain me why?

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

(or if should work, why isn't doing so in my code?)

先谢谢您

弗朗切斯科

...

[edit:刚刚注意到有人问过类似的问题,

[edit: just noticed that a similar question has been asked,

尽管如此,它仍然没有答案 从隔离函数中调用异步函数

nevertheless it is still unanswered Call async function from Isolate function,

github ]

推荐答案

忘记更新此:/ 如果您查看问题中链接的代码

forgot to update this :/ if you look at the code linked in the question

isolates_logging/lib/provider/test_isolate.dart

isolates_logging/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));部分

note the send.send(_syncHandle(data)); part

如果这样做,则只能发送基元,而不能发送期货, 基本上就是这样

if you do so, you can send only primitives and not futures, basically that's it

这篇关于飞镖/颤振:隔离的顶级功能的异步行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆