Last active
May 17, 2020 09:03
-
-
Save plateaukao/13a8de5eb838a2aa926e77b611f2a521 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 建立 loadbalancer object,指定想要產生的 isolate 數目 | |
Future<LoadBalancer> loadBalancer = LoadBalancer.create(8, IsolateRunner.spawn); | |
typedef Uint8List ImageProcessFunction(Uint8List bytes); | |
// 將 loadBalancer 相關操作包成 function。 這一步並非是必要的,可以直接使用 line 9 和 line 11。 | |
Future<Uint8List> loadBalancerRun(ImageProcessFunction function, Uint8List bytes) async { | |
// 在使用 loadBalancer 時,要先 await 一下,取得資源 | |
final lb = await loadBalancer; | |
// 呼叫 lb.run,執行原本 compute 在進行的工作 | |
return await lb.run<Uint8List, Uint8List>(function, bytes); | |
} | |
// 某個耗時的 function | |
Uint8List removeWhiteBackground(Uint8List bytes) { | |
... | |
} | |
// 程式碼中的使用方式。 | |
final image = await loadBalancerRun(removeWhiteBackground, response.bodyBytes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment