d-markey / squadron

Multithreading and worker thread pool for Dart / Flutter, to offload CPU-bound and heavy I/O tasks to Isolate or Web Worker threads.
https://pub.dev/packages/squadron
MIT License
79 stars 0 forks source link

would love to see an xplat "hello world" background task usage example #1

Closed csells closed 2 years ago

csells commented 2 years ago

I'm interested in seeing the simplest possible usage of squadron, e.g. what's the code needed to calculate 10K digits of pi (or whatever)? how do I write that in a cross-platform way across mobile, desktop and web?

d-markey commented 2 years ago

Hello Chris,

You asked for a minimal demo, here it is. The archive contains only the source code.

To try it out, create a new Flutter app (I named it "squadron_sample") and unzip the files attached.

The sample is based on Flutter's demo app, I just replaced the floating button with two buttons to test with / without workers and see the impact on the UI. The app computes 5K digits of pi (in hexadecimal) in two ways:

I have tested on Android and Web (Chrome). Dont have the tools for Windows and iOS.

In terms of architecture, you will find:

Don't hesitate to contact me if you need further help or info.

squadron_sample.zip

d-markey commented 2 years ago

One important thing: before you can run the Web version, you will need to pre-compile the Web worker:

dart compile js .\lib\src\browser\pi_worker_browser.dart -o .\web\pi_worker_browser.dart.js -m

I haven't figured out yet how to make Flutter do this automatically.

d-markey commented 2 years ago

Closing this issue, but don't hesitate to follow up should you have any comments!

d-markey commented 2 years ago

For anyone interested -- I've posted the sample on https://github.com/d-markey/squadron_sample and upgraded it to Squadron 3.1 including supports for task cancellation.

csells commented 2 years ago

nice!