Dart孤立的暂停功能未按预期工作 [英] Dart Isolates' pause function not working as expected

查看:89
本文介绍了Dart孤立的暂停功能未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩 Dart孤立并使用 isolate.pause(); 函数遇到了问题。

I've been playing around with Dart Isolates and have run into a problem using the isolate.pause(); function.

import 'dart:io';
import 'dart:isolate';

main(){
  ReceivePort receivePort = new ReceivePort();
  Isolate.spawn(isolateEntryPoint, receivePort.sendPort).then((isolate){
    isolate.pause(isolate.pauseCapability);
  });
}

void isolateEntryPoint(SendPort sendPort){
  while(true){
    print("isolate is running");
    sleep(new Duration(seconds: 2));
  }
}

在我的示例中,隔离区基本上只是在每个2秒

In my example the isolate basically just prints something out every 2 seconds.

根据我在文档,我的理解是上述代码应:

From what I read on the docs, my understanding is that the above code should:


  1. 生成隔离区

  2. 立即暂停隔离区

,但是它不起作用,隔离区仍在运行并打印 隔离正在运行(即使我告诉它暂停后也要每2秒运行一次)。

But it's not working, the isolate is still running and printing "isolate is running" every 2 seconds even after I tell it to pause.

我知道您可以通过传递 paused:true <来以暂停状态开始 / code>可选参数:
Isolate.spawn(isolateEntryPoint,receivePort,暂停:true)... 。但最终,我希望能够在任何时候都暂停隔离,而不仅仅是立即进行。

I'm aware that you can start an isolate in a paused state by passing in the paused: true optional parameter: Isolate.spawn(isolateEntryPoint, receivePort, paused: true).... Ultimately however I would like to be able to pause the isolate at any point, not just right off the bat.

我能找到的唯一使用此文档的地方是dart官方文档,因此我可能错误地使用了 isolate.pause()函数。但是无论哪种方式,都将非常感谢演示此功能正确用法的代码示例。

The only documentation I could find about using this was on the official dart docs, so it's possible I'm using the isolate.pause() function incorrectly. But either way a code example demonstrating the correct usage of this function would be greatly appreciated.

推荐答案

您是正确的,这不是

隔离暂停功能通过暂停事件队列起作用。当前正在执行的事件将完成,然后再恢复隔离之前,不会再处理其他事件。暂停不会影响正在运行的代码。

The isolate pause functionality works by pausing the event queue. The currently executing event will complete, and then no further events are processed until you resume the isolate. The pause does not affect running code.

在此代码中,隔离入口点正在运行带有内置延迟的无限循环。它永远不会返回到事件队列。如果您在循环中安排了任何异步操作,它们将永远不会执行。 sleep 原语使整个隔离区都处于睡眠状态,但这与不执行任何操作(只需花费更长的时间)没有什么不同。

In this code, the isolate entry point is running an infinite loop with a built-in delay. It never returns to the event queue. If you scheduled any asynchronous operations in the loop, they would never execute. The sleep primitive sleeps the entire isolate, but it's no different from doing nothing at all (it just takes longer).

您尝试立即暂停新隔离,但是隔离同时运行,并且当返回 Isolate 对象时,新隔离已经开始执行其入口点功能。

You try to "promptly pause" the new isolate, but isolates run concurrently, and the new isolate has already started executing its entry point function when the Isolate object is returned.

如果我们不必支持任何编译为JavaScript的隔离,将来可能会更改隔离功能,以便更渴望处理控制消息。更多信息,但是目前隔离的控制消息实际上是异步的,它们仅在Dart事件队列的事件之间生效。

It might be possible to change the isolate functionality in the future, to be more eager in handling control messages, if we don't have to support isolates compiled to JavaScript any more, but currently isolates control messages are effectively asynchronous, they only take effect between the events of the Dart event queue.

这篇关于Dart孤立的暂停功能未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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