Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What thread / isolate does flutter run IO operations on?

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); 
}

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

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

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?

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?

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?

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

like image 302
ams Avatar asked Jul 05 '19 16:07

ams


People also ask

How does isolate work in Flutter?

An isolate is an abstraction on top of threads. It is similar to an event loop, with a few differences: An isolate has its own memory space. It cannot share mutable values with other isolates.

What is main thread in Flutter?

At the point when Dart starts, there will be one main Isolate (Thread). It is the original main executing thread of the application, alluded to as the UI Thread. Isolates are: Dart's version of Threads. Isolate memory isn't shared with each other.

Is Dart single threaded?

Each Dart isolate has a single thread of execution and shares no mutable objects with other isolates. To communicate with each other, isolates use message passing.

How do you implement isolate in Flutter?

The first way to create an isolate is by using the Isolate. spawn() call. We pass in the method we want to run as the first parameter, while the second argument is the parameter we want to pass to the isolate.


1 Answers

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.

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.

like image 54
bulletProofCat Avatar answered Sep 28 '22 02:09

bulletProofCat