{"id":25391357,"url":"https://github.com/psastras/node-threadpool","last_synced_at":"2025-08-02T20:08:27.848Z","repository":{"id":142546913,"uuid":"138590101","full_name":"psastras/node-threadpool","owner":"psastras","description":"Node thread pools using worker threads","archived":false,"fork":false,"pushed_at":"2018-06-30T22:25:35.000Z","size":1353,"stargazers_count":30,"open_issues_count":7,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-05T15:09:44.559Z","etag":null,"topics":["node","thread","threadpool","typescript","workers"],"latest_commit_sha":null,"homepage":"https://psastras.github.io/node-threadpool/","language":"TypeScript","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/psastras.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-25T12:10:01.000Z","updated_at":"2024-04-02T00:04:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e567437-bc65-41f1-9214-4dd6ce9bde0b","html_url":"https://github.com/psastras/node-threadpool","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/psastras/node-threadpool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/psastras%2Fnode-threadpool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/psastras%2Fnode-threadpool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/psastras%2Fnode-threadpool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/psastras%2Fnode-threadpool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/psastras","download_url":"https://codeload.github.com/psastras/node-threadpool/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/psastras%2Fnode-threadpool/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268448204,"owners_count":24251999,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["node","thread","threadpool","typescript","workers"],"created_at":"2025-02-15T15:32:46.857Z","updated_at":"2025-08-02T20:08:27.825Z","avatar_url":"https://github.com/psastras.png","language":"TypeScript","readme":"# node-threadpool\n\n[![CircleCI](https://circleci.com/gh/psastras/node-threadpool.svg?style=svg)](https://circleci.com/gh/psastras/node-threadpool)\n[![npm version](https://badge.fury.io/js/node-threadpool.svg)](https://badge.fury.io/js/node-threadpool)\n[![codecov](https://codecov.io/gh/psastras/node-threadpool/branch/master/graph/badge.svg)](https://codecov.io/gh/psastras/node-threadpool)\n[![dependencies Status](https://david-dm.org/psastras/node-threadpool/status.svg)](https://david-dm.org/psastras/node-threadpool)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\n**WARNING: This project is mostly experimental and the API is subject to change.**\n\nThis package implements thread pools using node 10.5's new worker thread API (see: https://nodejs.org/api/worker_threads.html).\n\n## Features\n\n- Lightweight: one dependency (`surrial`) for serialization\n- Simple API: submit a function, await a result (no need to mess with loading from files, strings, etc.)\n- Supports transpiled code (ex: you may use Typescript to define your workers)\n- Can send most types of data including maps, sets, etc.\n- Supports shared data between threads, see the [example](#Shared-Data)\n\n## Why\n\nWorker threads are usually expensive to create, a thread pool maintains the threads and allows you to submit work on the fly, without having to pay the cost of recreating threads.\n\nWith node's new `worker_thread` API, threads in the pool can pass messages to each other and read and write to shared memory.\n\n## Usage\n\nFull API documentation can be found here: https://psastras.github.io/node-threadpool/api/modules/executors.html.\n\nIf you're familiar with Java's thread pool API, this should be very familiar:\n\n```javascript\nimport { Executors } from \"node-threadpool\";\n\nconst pool = Executors.newFixedThreadPool(1);\nconst result = pool.submit(async () =\u003e \"hello world\");\n\nconsole.log(await result); // prints \"hello world\"\n```\n\n**Requires node 10.5+. You must run node with the `--experimental-worker` flag enabled.**\n\n```\nNODE_OPTIONS=--experimental-worker ./server.js\n```\n\nor\n\n```\nnode --experimental-worker ./server.js\n```\n\n### Detailed Usage Instructions\n\nTo install:\n\n```sh\nyarn add node-threadpool\n```\n\nor\n\n```sh\nnpm install node-threadpool\n```\n\nImport `node-threadpool`:\n\n```javascript\nimport { Executors } from \"node-threadpool\";\n```\n\n[Executors](https://psastras.github.io/node-threadpool/api/modules/executors.html) contains methods to create different thread pools.\n\nCreate a thread pool by calling one of these methods:\n\n```javascript\n// creates a thread pool with 4 threads\nconst pool = Executors.newFixedThreadPool(4);\n```\n\nThen submit work to the pool with the `submit` method. This method takes in a function with no arguments that returns a Promise. The `submit` method itself returns a Promise which is resolved when the function has been executed.\n\n```javascript\n// these execute in parallel (as long as the pool size \u003e= 2)\nconst result1 = pool.submit(async () =\u003e \"done 1\");\nconst result2 = pool.submit(async () =\u003e \"done 2\");\n\nconsole.log(await result1); // joins and prints \"done1\"\nconsole.log(await result2); // joins and prints \"done2\"\n```\n\nSee the [documentation](https://psastras.github.io/node-threadpool/) for full API details.\n\nNote: if you're not using async / await, Promise based functions work just as well.\n\n### Warning\n\nYou may only access data within the runnable function's context. For example, this is an error:\n\n```javascript\nconst hello = \"hello\";\nawait pool.submit(async () =\u003e hello);\n```\n\nInstead, use the optional `data` object when submitting the function:\n\n```javascript\nconst hello = \"hello\";\nawait pool.submit(async (data) =\u003e data, hello);\n```\n\nSimilarly you must require third party modules from _inside_ the run method:\n\n```javascript\nawait pool.submit(async () =\u003e {\n  const fs = require('fs');\n  fs.readFileSync('README');\n});\n```\n\n## Examples\n\n### Basic Usage\n\n```typescript\nconst pool = Executors.newFixedThreadPool(4);\nconst result = pool.submit(async () =\u003e \"hello world\");\nconsole.log(await result); // prints \"hello world\"\n```\n\n### Pass Data\n\n```typescript\nconst pool = Executors.newSingleThreadedExecutor();\nconst map = new Map();\nmap.set(\"key\", \"value\");\nconst data = {\n  map\n};\nconst result = pool.submit(async d =\u003e d.map.get(\"key\"), data);\nconsole.log(await result); // prints \"value\"\n```\n\n### Shared Data\n\n#### Simple shared array buffer\n\n```typescript\nconst buffer = new SharedArrayBuffer(1 * Int32Array.BYTES_PER_ELEMENT);\nconst array = new Int32Array(sharedBuffer);\n\n// theres no lock, so in order to write safely we'll use one thread for this toy example\n// see the next example for atomic usage\nconst pool = Executors.newSingleThreadedExecutor();\n\n// set the data in the shared buffer to 42\nawait pool.submit(async d =\u003e (new Int32Array(d)[0] = 42), buffer);\n\n// read the data from the shared buffer\nconst result = pool.submit(async d =\u003e new Int32Array(d)[0], buffer);\n\nconsole.log(await result); // prints 42\n```\n\n#### Atomics\n\n```javascript\nconst buffer = new SharedArrayBuffer(1 * Int32Array.BYTES_PER_ELEMENT);\nconst array = new Int32Array(buffer);\nconst pool = Executors.newSingleThreadedExecutor();\n\nconst result = pool.submit(async d =\u003e {\n  const view = new Int32Array(d);\n  Atomics.wait(view, 0, 0); // wait here until the value is no longer 0\n  return Atomics.load(view, 0);\n}, buffer);\n\nAtomics.store(array, 0, 1); // change the value from 0, unblocking the worker thread\n\nconsole.log(await result); // prints 1\n```\n\n## TODOs\n\n- Figure out better function / data serialization.\n- Support cached thread executor\n- Clean up code\n- Settle on an API (when node's api is stable)\n- Support nested shared buffer serialization\n\n## License\n\nMIT Licensed, see the LICENSE file.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpsastras%2Fnode-threadpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpsastras%2Fnode-threadpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpsastras%2Fnode-threadpool/lists"}