颤动隔离与未来
Flutter Isolate vs Future
我对Isolate和Future的理解可能是错误的。请帮我清理一下。这是我对这两个主题的理解。
隔离:Isolates 运行 代码在它自己的事件循环中,每个事件可能 运行 嵌套微任务队列中的较小任务。
Future:Future 用于表示将来某个时间可用的潜在值或错误。
我的困惑是:
文档说 Isolate 有自己的循环?我觉得拥有自己的事件队列对我来说更有意义,我错了吗?
future 运行ning 会在主 Isolate 上异步吗?我假设未来的任务实际上被放置在事件队列的末尾,所以如果它将来会通过循环执行。如果我错了请纠正我。
有未来为什么要用Isolate?我看到一些示例使用 Isolate 代替 Future 来完成一些繁重的任务。但为什么?只有当未来在主隔离队列上异步执行时,它才对我有意义。
A Future
是一个句柄,允许您在异步执行完成时得到通知。
异步执行使用事件队列,代码在同一线程内并发执行。
https://webdev.dartlang.org/articles/performance/event-loop
Dart 代码默认在根隔离中执行。
您可以在另一个线程上启动通常 运行 的额外隔离。
一个 isolate 可以从根 isolate 开始时使用的相同 Dart 代码加载(具有与 main()
https://api.dartlang.org/stable/2.0.0/dart-isolate/Isolate/spawn.html) or with different Dart code (loaded from some Dart file or URL https://api.dartlang.org/stable/2.0.0/dart-isolate/Isolate/spawnUri.html 不同的入口点)。
Isolates 不共享任何状态,只能使用消息传递进行通信 (SendPort/ReceivePort)。每个 isolate 都有自己的事件队列。
即使 dart 不是多线程的,Isolate 也可以与 Thread 相提并论。当 Futures 共享相同的内存时,它确实有自己的内存和事件循环
Dart is able to spawn standalone processes, called Isolates (web workers in dart2js), which do not share memory when the main program, but are able to asynchronously, in another process (effectively a thread of sorts) is able to do computations without blocking the main thread.
Future 运行 在调用它的 Isolate 中,不一定是主隔离。
我推荐这个article,它比我解释得更好。
一句话我们可以说,
Isolates:Dart 是单线程的,但它能够使用 Isolates(许多进程)执行多线程操作。
Future:Future是dart完成一个异步工作时返回的结果。该工作通常在该单线程中完成。
在单个线程上隔离 运行s Dart 代码。同步代码如
print('hello');
立即运行,不能被打断。
Isolate 也有一个事件循环,用于安排异步任务。异步并不意味着这些任务 运行 在单独的线程上。他们仍然 运行 在同一个线程上。异步只是意味着它们被安排在以后。
事件循环 运行 是在所谓的事件队列中安排的任务。您可以通过创建这样的未来将任务放入事件队列中:
Future(() => print(hello));
当事件队列中位于它之前的其他任务完成时,print(hello)
任务将获得 运行。所有这一切都发生在同一个线程上,即同一个 Isolate。
有些任务不会立即添加到事件队列中,例如
Future.delayed(Duration(seconds: 1), () => print('hello'));
延迟一秒后才会添加到队列中。
到目前为止,我一直在谈论的所有内容都是在同一个线程、同一个 Isolate 上完成的。不过,有些工作实际上可能在不同的线程上完成,例如 IO 操作。底层框架负责这一点。如果在主 Isolate 线程上完成了诸如从磁盘读取之类的昂贵操作,那么它会阻塞应用程序直到它完成。当 IO 操作完成时,future 完成并将结果更新添加到事件队列中。
当您需要自己执行 CPU 密集型操作时,您应该 运行 在另一个隔离区进行操作,这样就不会在您的应用程序中造成卡顿。 compute 属性 对此很有用。您仍然使用 future,但这次 future 正在从不同的 Isolate 返回结果。
进一步研究
- Futures - Isolates - Event Loop
- Dart asynchronous programming: Isolates and event loops
- Are Futures in Dart threads?
- The Event Loop and Dart
- Flutter/Dart non-blocking demystify
- The Engine architecture
- Single Thread Dart, What? — Part 1
- Single Thread Dart, What? — Part 2
- Flutter Threading: Isolates, Future, Async And Await
- The Fundamentals of Zones, Microtasks and Event Loops in the Dart Programming Language
- An introduction to the dart:io library
我对Isolate和Future的理解可能是错误的。请帮我清理一下。这是我对这两个主题的理解。
隔离:Isolates 运行 代码在它自己的事件循环中,每个事件可能 运行 嵌套微任务队列中的较小任务。
Future:Future 用于表示将来某个时间可用的潜在值或错误。
我的困惑是:
文档说 Isolate 有自己的循环?我觉得拥有自己的事件队列对我来说更有意义,我错了吗?
future 运行ning 会在主 Isolate 上异步吗?我假设未来的任务实际上被放置在事件队列的末尾,所以如果它将来会通过循环执行。如果我错了请纠正我。
有未来为什么要用Isolate?我看到一些示例使用 Isolate 代替 Future 来完成一些繁重的任务。但为什么?只有当未来在主隔离队列上异步执行时,它才对我有意义。
A Future
是一个句柄,允许您在异步执行完成时得到通知。
异步执行使用事件队列,代码在同一线程内并发执行。
https://webdev.dartlang.org/articles/performance/event-loop
Dart 代码默认在根隔离中执行。
您可以在另一个线程上启动通常 运行 的额外隔离。
一个 isolate 可以从根 isolate 开始时使用的相同 Dart 代码加载(具有与 main()
https://api.dartlang.org/stable/2.0.0/dart-isolate/Isolate/spawn.html) or with different Dart code (loaded from some Dart file or URL https://api.dartlang.org/stable/2.0.0/dart-isolate/Isolate/spawnUri.html 不同的入口点)。
Isolates 不共享任何状态,只能使用消息传递进行通信 (SendPort/ReceivePort)。每个 isolate 都有自己的事件队列。
即使 dart 不是多线程的,Isolate 也可以与 Thread 相提并论。当 Futures 共享相同的内存时,它确实有自己的内存和事件循环
Dart is able to spawn standalone processes, called Isolates (web workers in dart2js), which do not share memory when the main program, but are able to asynchronously, in another process (effectively a thread of sorts) is able to do computations without blocking the main thread.
Future 运行 在调用它的 Isolate 中,不一定是主隔离。
我推荐这个article,它比我解释得更好。
一句话我们可以说,
Isolates:Dart 是单线程的,但它能够使用 Isolates(许多进程)执行多线程操作。
Future:Future是dart完成一个异步工作时返回的结果。该工作通常在该单线程中完成。
在单个线程上隔离 运行s Dart 代码。同步代码如
print('hello');
立即运行,不能被打断。
Isolate 也有一个事件循环,用于安排异步任务。异步并不意味着这些任务 运行 在单独的线程上。他们仍然 运行 在同一个线程上。异步只是意味着它们被安排在以后。
事件循环 运行 是在所谓的事件队列中安排的任务。您可以通过创建这样的未来将任务放入事件队列中:
Future(() => print(hello));
当事件队列中位于它之前的其他任务完成时,print(hello)
任务将获得 运行。所有这一切都发生在同一个线程上,即同一个 Isolate。
有些任务不会立即添加到事件队列中,例如
Future.delayed(Duration(seconds: 1), () => print('hello'));
延迟一秒后才会添加到队列中。
到目前为止,我一直在谈论的所有内容都是在同一个线程、同一个 Isolate 上完成的。不过,有些工作实际上可能在不同的线程上完成,例如 IO 操作。底层框架负责这一点。如果在主 Isolate 线程上完成了诸如从磁盘读取之类的昂贵操作,那么它会阻塞应用程序直到它完成。当 IO 操作完成时,future 完成并将结果更新添加到事件队列中。
当您需要自己执行 CPU 密集型操作时,您应该 运行 在另一个隔离区进行操作,这样就不会在您的应用程序中造成卡顿。 compute 属性 对此很有用。您仍然使用 future,但这次 future 正在从不同的 Isolate 返回结果。
进一步研究
- Futures - Isolates - Event Loop
- Dart asynchronous programming: Isolates and event loops
- Are Futures in Dart threads?
- The Event Loop and Dart
- Flutter/Dart non-blocking demystify
- The Engine architecture
- Single Thread Dart, What? — Part 1
- Single Thread Dart, What? — Part 2
- Flutter Threading: Isolates, Future, Async And Await
- The Fundamentals of Zones, Microtasks and Event Loops in the Dart Programming Language
- An introduction to the dart:io library