{"id":19937082,"url":"https://github.com/farhadi/task-pool","last_synced_at":"2025-06-11T18:09:37.225Z","repository":{"id":65514708,"uuid":"11502713","full_name":"farhadi/task-pool","owner":"farhadi","description":"A generic pool to limit number of running asynchronous tasks in node.js","archived":false,"fork":false,"pushed_at":"2016-07-01T12:04:54.000Z","size":7,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-03T00:38:17.776Z","etag":null,"topics":["async","concurrency","promise","task-pool"],"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/farhadi.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":"2013-07-18T12:49:20.000Z","updated_at":"2024-02-19T09:53:45.000Z","dependencies_parsed_at":"2023-01-26T21:05:12.147Z","dependency_job_id":null,"html_url":"https://github.com/farhadi/task-pool","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhadi%2Ftask-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhadi%2Ftask-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhadi%2Ftask-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhadi%2Ftask-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farhadi","download_url":"https://codeload.github.com/farhadi/task-pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252203183,"owners_count":21710902,"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":["async","concurrency","promise","task-pool"],"created_at":"2024-11-12T23:30:48.656Z","updated_at":"2025-05-03T14:31:01.774Z","avatar_url":"https://github.com/farhadi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"task-pool\n=========\nA generic pool to limit number of running asynchronous tasks or promises.\n\n[![Build Status](https://travis-ci.org/farhadi/task-pool.png)](https://travis-ci.org/farhadi/task-pool)\n[![Coverage Status](https://coveralls.io/repos/github/farhadi/task-pool/badge.svg?branch=master)](https://coveralls.io/github/farhadi/task-pool?branch=master)\n\nInstallation\n------------\n\n    npm install task-pool\n\nUsage\n-----\n\nConsider you are going to do lots of asynchrounous CPU/IO intensive tasks.\nLets say running a huge number of external tasks using exec calls, or reading a lot of files in a loop.\nIn this case you may want to limit the number of running tasks at a time to avoid high load and memory usages.\n\ntask-pool does the job for you. It makes a queue of tasks and manages to run only a limited number of them at a time.\n\n``` javascript\nvar Pool = require('task-pool').Pool;\n\n//Create a new pool with a maximum number of 20 tasks at a time.\n//Tasks taking longer than 5 seconds continue their journey out of the pool to leave space for new tasks.\nvar pool = new Pool({limit: 20, timeout: 5000});\n\n//Create a wrapper around exec\nvar exec = pool.wrap(require('child_process').exec);\n\n//An array of commands to be executed using exec.\nvar tasks = [ ... ];\nfor (i = 0; i \u003c tasks.length; i++) {\n    exec(tasks[i], function(error, stdout, stderr) {\n        //Do some process on the results\n    });\n}\n```\n\nAs you can see exec wrapper acts like the original exec method. The only exception is that it doesn't return the ChildProcess object immediately. It returns a `Task` object.\nFor example if you want to grab the pid of the child process, do it like this:\n\n```javascript\nvar task = exec(tasks[i], function(error, stdout, stderr) {\n    //Do some process on the results\n});\ntask.on('run', function(child) {\n    console.log(child.pid);\n});\n```\n\nYou can also wrap a function that returns a promise:\n\n``` javascript\n//Create a wrapper around exec\nvar exec = pool.promise.wrap(require('child-process-promise').exec);\n\n//An array of commands to be executed using exec.\nvar tasks = [ ... ];\nfor (i = 0; i \u003c tasks.length; i++) {\n    exec(tasks[i]).then(result) {\n        //Do some process on the result\n    }).catch(error, function(error) {\n        //Do something when an error occurs\n    });\n}\n```\n\nLicense\n-------\ntask-pool is released under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarhadi%2Ftask-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarhadi%2Ftask-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarhadi%2Ftask-pool/lists"}