抖动在哪个线程/隔离上运行IO操作? [英] What thread / isolate does flutter run IO operations on?

查看:97
本文介绍了抖动在哪个线程/隔离上运行IO操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用http程序包或进行常规IO操作时,情况会变得扑朔迷离

In flutter when using the http package or doing general IO operations for example

import 'package:http/http.dart' as http;

http.Response response = await http.get(url);
if (response.statusCode == 200) {
  var json = jsonDecode(response.body); 
}

我已阅读引擎架构,它指示引擎中有4个线程

I have read through The engine architecture which indicates there are 4 threads in the engine


  • 平台任务运行器

  • UI任务运行器

  • GPU任务运行器

  • IO Task Runner

  • Platform Task Runner
  • UI Task Runner
  • GPU Task Runner
  • IO Task Runner

主应用程序飞镖代码在UI Task Runner线程上运行。 IO任务运行程序似乎仅用于dart引擎读取图像来处理耗时的图像IO,而不是应用程序IO发生的位置?

The main app dart code runs on the UI Task Runner Thread. The IO task runner seems to be only for the dart engine to read images handle time consuming image IO and not where application IO happens?

我了解IO库没有-阻塞基于Future的接口,因此我提供给IO库的回调将在UI线程上运行,但是实际的IO操作本身如何,Dart VM正在使用OS线程来执行这些操作?

I understand that the IO libraries have no-blocking Future based interfaces so the callbacks I provide to the IO libraries will run on the UI thread but what about the actual IO operations themselves is there an OS thread that the Dart VM is using to do these operations?

例如,如果我尝试上传/下载800MB视频文件,Dart VM使用的后台IO线程是否有实际的IO?

For example if I try to upload/download an 800MB video file is there a background IO thread that the Dart VM uses do the actual IO?

是否应将单独的隔离区用于大型IO操作(如上载/下载大文件)?

Should a separate isolate be used for large IO operations like uploading / downloading large files?

推荐答案

Dart使用线程池处理IO请求。为了找出答案,我不得不克隆Dart SDK并查看源代码,因为我无法从文档中找到答案。

Dart handles IO requests with a thread pool. To find out I had to clone the Dart SDK and look into the source code, as I couldn't find answer from the docs.

调用IO方法时,文件实现 _File 类方法被调用。它创建一个到本机代码的端口( IOService_NewServicePort ),并将IO请求ID和args发送到本机代码。本机代码使用线程池处理IO请求( runtime\vm\native_api_impl.cc#Dart_NewNativePort ),将任务提交到线程池中。然后,本机代码一直返回到Dart代码,而 _File 返回将来的对象。 IO操作完成后,结果通过之前创建的端口从本地发送回Dart。这会触发在端口上注册的处理程序,并且将来会解决。

When an IO method is called, the File implementation _File class method is called. It creates a Port to native code (IOService_NewServicePort) and sends the IO request id and args to native code. The native code handles the IO requests with a thread pool (runtime\vm\native_api_impl.cc#Dart_NewNativePort), submiting a task into the thread pool. Then the native code returns all the way back to Dart code and _File returns a future object. After the IO operation is done, the result is sent back from native to Dart by the port created before. This triggers a handler registered on the port and the future is resolved.

这篇关于抖动在哪个线程/隔离上运行IO操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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