{"id":26485426,"url":"https://github.com/shahidcodes/threadifier-node","last_synced_at":"2025-03-20T05:57:56.031Z","repository":{"id":36835220,"uuid":"230636047","full_name":"shahidcodes/threadifier-node","owner":"shahidcodes","description":"Run long running task in worker thread with promises.","archived":false,"fork":false,"pushed_at":"2022-02-12T21:33:51.000Z","size":297,"stargazers_count":7,"open_issues_count":6,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-25T04:05:42.040Z","etag":null,"topics":["creating-threads","multithreading","nodejs","promises","thread-pool","worker-thread"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shahidcodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-28T16:43:14.000Z","updated_at":"2024-02-17T13:45:30.000Z","dependencies_parsed_at":"2022-08-08T17:31:27.536Z","dependency_job_id":null,"html_url":"https://github.com/shahidcodes/threadifier-node","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/shahidcodes%2Fthreadifier-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahidcodes%2Fthreadifier-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahidcodes%2Fthreadifier-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahidcodes%2Fthreadifier-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shahidcodes","download_url":"https://codeload.github.com/shahidcodes/threadifier-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244166715,"owners_count":20409178,"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":["creating-threads","multithreading","nodejs","promises","thread-pool","worker-thread"],"created_at":"2025-03-20T05:57:55.545Z","updated_at":"2025-03-20T05:57:56.024Z","avatar_url":"https://github.com/shahidcodes.png","language":"JavaScript","readme":"# Threadifier\n\n### Motivation\n\nNode has recently added worker_thread, giving us ability to run long running synchronous tasks in a separate thread. But API is not very seamless.  \nThreadifier allows you to run any function in different thread seamlessly through its easy to use promise based api. It uses `worker_threads` module to run the given function in a new thread and return the response asynchronously so your event loop keeps running. Also you can create a worker thread pool to reuse the same threads for various tasks.\n\n## Get started\n\n    npm i @shahidcodes/threadifier\n\n## Usage\n\n### Run a function in different thread.\n\n```javascript\nconst Threadifier = require('@shahidcodes/threadifier');\n\nfunction runTask(arg) {\n  let i = 0;\n  for (let index = 0; index \u003c 99999999; index++) {\n    i++;\n  }\n  return `worked- loop count ${i}, arg: ${JSON.stringify(arg)}`;\n}\n\nconst args = { name: 'Shahid' };\n\nThreadifier.run(runTask, args)\n  .then(console.log)\n  .catch(console.error);\n```\n\n### Create a pool of threads and queue the tasks (recommended)\n\n```javascript\nconst { WorkerPool } = require('@shahidcodes/threadifier');\n\nconst pool = new WorkerPool(10);\n\nfunction runTask(args) {\n  let i = 0;\n  for (let index = 0; index \u003c 999999999; index++) {\n    i++;\n  }\n  return { i, args };\n}\n\nconst args = { name: 'Shahid' };\n\nfor (let index = 0; index \u003c 100; index++) {\n  pool\n    .queueTask(runTask, args)\n    .then(result =\u003e console.log(`from worker:`, result))\n    .catch(console.error);\n}\n```\n\n## General Advice\n\nYou should not create many threads at once. If you're going to use this in a request handler/controller then better use a fixed size thread pool. And queue tasks otherwise you'll see CPU spikes when you spawn many many threads than the server can handle. One other reason you should use a thread pool is creating worker thread is expensive so if you're creating threads again n again then better use pool.\n\n## Examples\n\nPlease check the [examples](./examples) directory.\n\n## Contribution\n\nYou're free to submit PRs. There are no build/tests yet. But it is in roadmap.\n\n## Contact\n\nFeel free to contact me on [twitter.com/shahidcodes](https://twitter.com/shahidcodes)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshahidcodes%2Fthreadifier-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshahidcodes%2Fthreadifier-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshahidcodes%2Fthreadifier-node/lists"}