{"id":17816457,"url":"https://github.com/zebrajaeger/threadpool","last_synced_at":"2025-10-04T20:27:46.676Z","repository":{"id":57170440,"uuid":"286248813","full_name":"zebrajaeger/threadpool","owner":"zebrajaeger","description":"js threadpool","archived":false,"fork":false,"pushed_at":"2020-09-04T13:42:25.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T17:03:20.685Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/zebrajaeger.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}},"created_at":"2020-08-09T14:04:16.000Z","updated_at":"2020-09-04T13:42:00.000Z","dependencies_parsed_at":"2022-08-27T13:11:43.840Z","dependency_job_id":null,"html_url":"https://github.com/zebrajaeger/threadpool","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebrajaeger%2Fthreadpool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebrajaeger%2Fthreadpool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebrajaeger%2Fthreadpool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zebrajaeger%2Fthreadpool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zebrajaeger","download_url":"https://codeload.github.com/zebrajaeger/threadpool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246779914,"owners_count":20832485,"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":[],"created_at":"2024-10-27T16:38:11.930Z","updated_at":"2025-10-04T20:27:41.623Z","avatar_url":"https://github.com/zebrajaeger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# threadpool\n\n[![NPM Version](https://img.shields.io/npm/v/@zebrajaeger/threadpool.svg?style=flat)](https://www.npmjs.org/package/@zebrajaeger/threadpool)\n[![Install Size](https://packagephobia.now.sh/badge?p=@zebrajaeger/threadpool)](https://packagephobia.now.sh/result?p=@zebrajaeger/threadpool)\n[![License](https://img.shields.io/github/license/zebrajaeger/threadpool)](https://img.shields.io/github/license/zebrajaeger/threadpool)\n\njs threadpool\n\n## Example\n\n### Pool Code\n\n``` javascript\nconst path = require('path')\nconst {StaticWorkerPool} = require('@zebrajaeger/threadpool')\n\nconst threadFile = path.resolve(__dirname, 'mythread.js');\n(async () =\u003e {\n    // create pool (default count = os.cpus().length)\n    const p = new StaticWorkerPool(threadFile).begin();\n\n    // start 20 jobs\n    for (let i = 1; i \u003c= 20; ++i) {\n        p.exec(`foo(${i})`).promise.then(r =\u003e console.log('xxx', r))\n    }\n\n    // wait until all jobs are done\n    await p.finished();\n    console.log('DONE')\n\n    // shutdown pool\n    await p.destroy();\n    console.log('Destroyed')\n})();\n```\n\n### Worker Code (mythread.js)\n\n``` javascript\nconst {parentPort} = require('worker_threads');\n\n// for every worker this is a new instance \n// so this 'counter' starts everytime with zero.\nlet counter = 0;\n\n// the code to execute: \n// - wait a little bit and then return result\n// - on every 5th execution fail \nasync function toExec(msg) {\n    return new Promise((resolve, reject) =\u003e {\n        setTimeout(() =\u003e {\n            if (++counter \u003e= 5) {\n                counter = 0;\n                reject('Oh no!!');\n            } else {\n                resolve(`Meep(${counter}): '${msg}'`);\n            }\n        }, 100)\n    })\n}\n\n// entry point for WorkerPool/WorkerThread\nparentPort.on('message', async data =\u003e {\n    // wrap errors. Not needed but more easy to handle\n    try {\n        parentPort.postMessage({result: await toExec(data), data});\n    } catch (error) {\n        parentPort.postMessage({error, data});\n    }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzebrajaeger%2Fthreadpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzebrajaeger%2Fthreadpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzebrajaeger%2Fthreadpool/lists"}