asci
asci

Reputation: 1

How to efficiently use multiple isolates in Dart (Flutter)

I want to create a large-scale app using Isolate. It is an app with many tasks such as API calls, DB queries, and file R/W. Currently, I have implemented it in a way that it receives requests from multiple places in one Isolate and returns them sequentially. (Implemented similarly to https://medium.com/@danger-ahead/working-with-isolates-and-hive-in-flutter-b5ef3d32fa2a),

1. Isolate create
2. initialization (DB / SharedPreference / EventBus initialization, etc. - slightly expensive)
3. Request waiting
4. Response after processing
5. Repeat 3-4 processes in order of requests until app termination.

I'm thinking about restructuring my app by using multiple isolates instead of a single isolate, referring to Multiple Isolates vs one Isolate.

In other languages (such as Java), it is possible to share the heap between threads, so after initialization, it is stored in the heap, and then whenever a request comes in, I remember being able to create a thread and access the heap to process it and then respond. Dart's In this case, it is impossible to share the heap between isolates, so I think it will be difficult to handle it this way.

So I can think of three methods below, but please advise which method is usually used, or if the approach is wrong.

  1. Process all responses using one Isolate (now)
  2. Create an Isolate whenever a request comes create -> initialization -> processing -> destruction
  3. Create as many Isolates as the number of cores in advance and process the requests in a distributed manner (terminate when the app is terminated)

Thank you for your interest.

Upvotes: 0

Views: 160

Answers (0)

Related Questions