Flutter demo for Squadron with sample use-cases:
- parallel computation of hexadecimal digits of Pi.
- parallel thumbnail generation
Also includes a minimal example of using Squadron annotations together with squadron_builder to generate the actual Web Worker/Isolate workers and pools.
See Squadron Dart package for more information on Squadron.
The source code should be easy to understand:
-
pi_digits_service.dartimplements the math to compute hexadecimal digits of Pi based on the Bailey–Borwein–Plouffe formula. -
pi_digits_worker.dartimplements the code used to proxy calls from the main thread to the platform workers (Isolates in native scenarios,WebWorkers in browser environment). -
pi_worker_pool.dartimplements the worker pool depending on the target platform (browser or native). It will instantiate workers either frombrowser/pi_digits_worker.dartor fromvm/pi_digits_worker.dartvia the correspondingpi_digits_worker_activator.dart. -
browser/pi_digits_worker.dartandvm/pi_digits_worker.dartinstall and run the worker for their respective platform, relying on the service implemented inpi_digits_service.dart.
Squadron takes care of all the rest: bootstrapping the workers, communicating to/from the workers, distributing tasks to the worker service, cancellation notifications...
The main app uses the worker pool to have 5,000 digits of Pi computed in parallel (see _loadDigitsWithWorkerPool() in pi_digits_page.dart).
The structure of the code is very similar to that for pi digits:
-
thumbnail_service.dartimplements the generation of the thumbnail via image. -
thumbnail_worker.dartimplements the code used to proxy calls from the main thread to the platform workers (Isolates in native scenarios,WebWorkers in browser environment). -
thumbnail_pool.dartimplements the worker pool depending on the target platform (browser or native). It will instantiate workers either frombrowser/thumbnail_worker.dartor fromvm/thumbnail_worker.dartvia the correspondingthumbnail_worker_activator.dart. -
browser/thumbnail_worker.dartandvm/thumbnail_worker.dartinstall and run the worker for their respective platform, relying on the service implemented inthumbnail_service.dart.
Squadron takes care of all the rest: bootstrapping the workers, communicating to/from the workers, distributing tasks to the worker service, cancellation notifications...
The main app uses the Flutter icon as a sample image. In a production app, you'd ask the user to pick images or take pictures with the camera.
Provided as examples of code generation using Squadron annotations with squadron_builder.
Codegen is a basic example while Perf leverages more advanced features such as platform-specific marshallers to try and control serialization overhead.
To generate the code for workers and worker pools, run the following command:
dart run build_runner build
Various attempts at parsing a (somewhat large) Value Change Dump dataset (see https://en.wikipedia.org/wiki/Value_change_dump).
This sample must be built using:
dart run build_runner build
Before running the Web-version of the sample, the Web workers must also be compiled. The sample provides a script to automate building the Web workers, cf. build_web_worker.bat. Rebuilding Web Workers is mandatory whenever a worker service or one of its dependencies has been modified or updated.