{"id":16647634,"url":"https://github.com/sachaarbonel/worker","last_synced_at":"2026-01-11T04:38:54.556Z","repository":{"id":59150181,"uuid":"172063752","full_name":"sachaarbonel/Worker","owner":"sachaarbonel","description":"A dart 2 fork of Worker","archived":false,"fork":false,"pushed_at":"2019-07-04T18:59:27.000Z","size":7215,"stargazers_count":10,"open_issues_count":3,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-13T10:07:34.908Z","etag":null,"topics":["cpu","dart","futures","isolate","parallel"],"latest_commit_sha":null,"homepage":"https://github.com/Dreckr/Worker","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sachaarbonel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-22T12:36:32.000Z","updated_at":"2021-10-04T14:46:31.000Z","dependencies_parsed_at":"2022-09-13T08:50:24.520Z","dependency_job_id":null,"html_url":"https://github.com/sachaarbonel/Worker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sachaarbonel%2FWorker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sachaarbonel%2FWorker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sachaarbonel%2FWorker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sachaarbonel%2FWorker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sachaarbonel","download_url":"https://codeload.github.com/sachaarbonel/Worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230614079,"owners_count":18253711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpu","dart","futures","isolate","parallel"],"created_at":"2024-10-12T08:45:20.772Z","updated_at":"2026-01-11T04:38:54.531Z","avatar_url":"https://github.com/sachaarbonel.png","language":"Dart","readme":"Worker\n=====\n\nAn easy to use utility to perform tasks concurrently.\n\nBy performing blocking CPU intensive tasks concurrently, you free your main isolate \nto do other stuff while you take advantage of the CPU capabilities.\n\nUsage\n-----\n\nTo use this library, create a new Worker that will handle the isolates for you, \nencapsulate your task in class that implements the Task interface and pass it to the Worker \nto execute it:\n\n```dart\nvoid main () {\n\tWorker worker = new Worker();\n\tTask task = new AckermannTask(1, 2);\n\tworker.handle(task).then((result) =\u003e print(result));\n}\n\nclass AckermannTask implements Task {\n  int x, y;\n\n  AckermannTask (this.x, this.y);\n\n  int execute () {\n    return ackermann(x, y);\n  }\n\n  int ackermann (int m, int n) {\n    if (m == 0)\n      return n+1;\n    else if (m \u003e 0 \u0026\u0026 n == 0)\n      return ackermann(m-1, 1);\n    else\n      return ackermann(m-1, ackermann(m, n-1));\n  }\n}\n```\n\nYou can also define how many tasks a worker can execute concurrently and if the isolates\nshould be spawned lazily:\n\n```dart\nWorker worker = new Worker(poolSize: 4, spawnLazily: false);\n```\nIf you want to manage the isolates and SendPorts yourself but still use Tasks,\nWorkerIsolate comes to the rescue:\n\n```dart\nWorkerIsolate isolate = new WorkerIsolate();\nisolate.performTask(new AckermannTask(1, 2))\n\t\t.then(doSomethingAwesome);\n```\n\nYou can perform multple http get requests in parallel for example :\n```dart\nimport 'dart:async';\nimport 'package:worker2/worker2.dart';\nimport 'dart:io';\nimport 'dart:convert';\n\nvoid main() {\n  final worker = new Worker(poolSize: 4, spawnLazily: false);\n  var futures = \u003cFuture\u003e[];\n  \n  for (var url in [\"https://swapi.co/api/people/1\",\"https://swapi.co/api/people/2\",\"https://swapi.co/api/people/3\"]) {\n    futures.add(worker.handle(AsyncHttpGet(url)));\n  }\n\n  Future.wait(futures).then((r) {\n    print(r);\n    worker.close();\n  });\n\n}\n\nFuture\u003cString\u003e getUrl(String url) async {\n  Completer completer = new Completer();\n  StringBuffer contents = new StringBuffer();\n  var request = await HttpClient().getUrl(Uri.parse(url));\n  var response = await request.close(); \n response.transform(utf8.decoder).listen((String data) {\n    contents.write(data);\n  }, onDone: () =\u003e completer.complete(contents.toString()));\n  return await completer.future;\n}\n\nclass AsyncHttpGet implements Task\u003cFuture\u003cString\u003e\u003e {\n  String url;\n  bool throwException;\n\n  AsyncHttpGet (this.url, {this.throwException: false});\n\n  Future\u003cString\u003e execute () {\n    return getUrl(url);\n  }\n}\n```\n\nTips\n----\n* A Task may return a Future. The Worker will wait until this Future is completed and will return its result.\n* If you have to perform many iterations of an operation, you can batch it into tasks and run them concurrently.\n* Running tasks in other isolates involves copying the task object to the other isolate. Keep your task thin.\n* Always use benchmarks to identify if Worker is really helping and if the amount of isolates is ideal.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsachaarbonel%2Fworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsachaarbonel%2Fworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsachaarbonel%2Fworker/lists"}