{"id":26257152,"url":"https://github.com/smoren/multiprocessor-ts","last_synced_at":"2025-10-30T17:49:48.498Z","repository":{"id":270986748,"uuid":"912038003","full_name":"Smoren/multiprocessor-ts","owner":"Smoren","description":"Multiprocessing pool for NodeJS and TypeScript","archived":false,"fork":false,"pushed_at":"2025-01-21T16:17:21.000Z","size":24,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-28T15:15:42.595Z","etag":null,"topics":["child-process","child-process-pool","map","map-unordered","multicore","multicore-cpu","multiprocess","multiprocessing","parallel","pool","worker","workers"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/multiprocessor","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/Smoren.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":"2025-01-04T14:20:27.000Z","updated_at":"2025-03-30T23:45:39.000Z","dependencies_parsed_at":"2025-01-04T15:42:03.598Z","dependency_job_id":"5dbf742d-7d30-4229-b38b-41901006eb40","html_url":"https://github.com/Smoren/multiprocessor-ts","commit_stats":null,"previous_names":["smoren/multiprocessor-ts"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fmultiprocessor-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fmultiprocessor-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fmultiprocessor-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fmultiprocessor-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Smoren","download_url":"https://codeload.github.com/Smoren/multiprocessor-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251336389,"owners_count":21573188,"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":["child-process","child-process-pool","map","map-unordered","multicore","multicore-cpu","multiprocess","multiprocessing","parallel","pool","worker","workers"],"created_at":"2025-03-13T20:28:42.601Z","updated_at":"2025-10-30T17:49:43.477Z","avatar_url":"https://github.com/Smoren.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multiprocessing Pool Implementation for TypeScript\n\n[![npm](https://img.shields.io/npm/v/multiprocessor.svg)](https://www.npmjs.com/package/multiprocessor)\n[![npm](https://img.shields.io/npm/dm/multiprocessor.svg?style=flat)](https://www.npmjs.com/package/multiprocessor)\n[![Coverage Status](https://coveralls.io/repos/github/Smoren/multiprocessor-ts/badge.svg?branch=master\u0026rand=222)](https://coveralls.io/github/Smoren/multiprocessor-ts?branch=master)\n![Build and test](https://github.com/Smoren/multiprocessor-ts/actions/workflows/test.yml/badge.svg)\n[![Minified Size](https://badgen.net/bundlephobia/minzip/multiprocessor)](https://bundlephobia.com/result?p=multiprocessor)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nDescription\n-----------\n\nMultiprocessing pool implementation for NodeJS and TypeScript.\n\nReal multiprocessing is implemented using [child_process](https://nodejs.org/api/child_process.html) module.\n\nSetup\n-----\n\n```bash\nnpm i multiprocessor\n```\n\nUsage example\n-------------\n\n```typescript\nimport { Pool } from 'multiprocessor';\n\nconst poolSize = 4;\n\nconst pool = new Pool(poolSize);\nconst input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n\nconst result = await pool.map(input, calcSinTask, {\n  onTaskSuccess: (result: number, input: number, index: number) =\u003e {\n    console.log(`Task #${index} | result: ${result}, input: ${input}`);\n  },\n  onTaskError: (error: string, input: number, index: number) =\u003e {\n    console.log(`Task #${index} | error: ${error}, input: ${input}`);\n  }\n});\npool.close();\n\nconsole.log(result);\n// [ 0.8414, 0.9092, 0.1411, ... ]\n\nfunction calcSinTask(x: number): number {\n  let result = 0;\n  let sign = 1;\n  let power = x;\n  let factorial = 1;\n\n  for (let n = 0; n \u003c 1000000; n++) {\n    if (n \u003e 0) {\n      factorial *= (2 * n) * (2 * n + 1);\n      power *= x * x;\n      sign *= -1;\n    }\n\n    const delta = sign * (power / factorial);\n\n    if (isNaN(result + delta)) {\n      return result\n    }\n\n    result += delta;\n  }\n\n  return result;\n}\n```\n\n### Example with import\n\nYou can run this example from [this repository](https://github.com/Smoren/multiprocessor-example-ts).\n\n```typescript\n// File: src/index.ts\nimport { Pool } from 'multiprocessor';\n\nconst poolSize = 4;\n\nconst pool = new Pool(poolSize);\nconst input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n\nconst result = await pool.map(input, calcSinTask, {\n  onTaskSuccess: (result: number, input: number, index: number) =\u003e {\n    console.log(`Task #${index} | result: ${result}, input: ${input}`);\n  },\n  onTaskError: (error: string, input: number, index: number) =\u003e {\n    console.log(`Task #${index} | error: ${error}, input: ${input}`);\n  }\n});\npool.close();\n\nconsole.log(result);\n// [ 0.8414, 0.9092, 0.1411, ... ]\n\nasync function calcSinTask(x: number): Promise\u003cnumber\u003e {\n  const dirName = __dirname.replace('/node_modules/multiprocessor/lib', '/src');\n  const { calcSin } = await import(`${dirName}/path/to/your/module`);\n  return calcSin(x);\n}\n```\n\n```typescript\n// File: src/path/to/your/module.ts\nexport function calcSin(x: number): number {\n  let result = 0;\n  let sign = 1;\n  let power = x;\n  let factorial = 1;\n\n  for (let n = 0; n \u003c 1000000; n++) {\n    if (n \u003e 0) {\n      factorial *= (2 * n) * (2 * n + 1);\n      power *= x * x;\n      sign *= -1;\n    }\n\n    const delta = calcDelta(sign, power, factorial);\n\n    if (isNaN(result + delta)) {\n      return result\n    }\n\n    result += delta;\n  }\n\n  return result;\n}\n\nfunction calcDelta(sign: number, power: number, factorial: number): number {\n  return sign * (power / factorial);\n}\n```\n\nAPI Reference\n-------------\n\nFor detailed documentation and usage examples, please refer to the [API documentation](https://smoren.github.io/multiprocessor-ts/).\n\n## Types\n```typescript\nexport type Task\u003cTInput, TResult\u003e = (input: TInput) =\u003e (Promise\u003cTResult\u003e | TResult);\nexport type TaskSuccessHandler\u003cTInput, TResult\u003e = (result: TResult, input: TInput, index: number) =\u003e void;\nexport type TaskErrorHandler\u003cTInput\u003e = (error: string, input: TInput, index: number) =\u003e void;\nexport type TaskResponse\u003cTResult\u003e = [number, TResult | undefined, string | undefined];\n```\n\n## Pool\n```typescript\nclass Pool extends EventEmitter {\n  /**\n   * Create a new pool with the specified number of workers.\n   *\n   * @param poolSize The number of workers to create in the pool.\n   */\n  constructor(poolSize: number);\n\n  /**\n   * Asynchronously processes tasks from the provided inputs in an ordered manner.\n   * Tasks are executed concurrently using a pool of workers.\n   *\n   * @template TInput The type of the input elements.\n   * @template TResult The type of the result elements.\n   *\n   * @param inputs An iterable or async iterable of input elements.\n   * @param task The task to execute for each input element.\n   * @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).\n   *\n   * @returns A promise that resolves to an array of task results in the order of the input elements.\n   */\n  public async map\u003cTInput, TResult\u003e(\n    inputs: Iterable\u003cTInput\u003e | AsyncIterable\u003cTInput\u003e,\n    task: Task\u003cTInput, TResult\u003e,\n    taskHandlers?: TaskHandlers\u003cTInput, TResult\u003e,\n  ): Promise\u003cArray\u003cTResult | undefined\u003e\u003e;\n\n  /**\n   * Asynchronously processes tasks from the provided inputs in a lazy ordered manner.\n   * Tasks are executed concurrently using a pool of workers.\n   *\n   * @template TInput The type of the input elements.\n   * @template TResult The type of the result elements.\n   *\n   * @param inputs An iterable or async iterable of input elements.\n   * @param task The task to execute for each input element.\n   * @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).\n   *\n   * @returns An async generator yielding results of the tasks in the order of the input elements.\n   */\n  public async *imap\u003cTInput, TResult\u003e(\n    inputs: Iterable\u003cTInput\u003e | AsyncIterable\u003cTInput\u003e,\n    task: Task\u003cTInput, TResult\u003e,\n    taskHandlers?: TaskHandlers\u003cTInput, TResult\u003e,\n  ): AsyncGenerator\u003cTResult | undefined\u003e;\n\n  /**\n   * Asynchronously processes tasks from the provided inputs in a lazy unordered manner.\n   * Tasks are executed concurrently using a pool of workers.\n   *\n   * @template TInput The type of the input elements.\n   * @template TResult The type of the result elements.\n   *\n   * @param inputs An iterable or async iterable of input elements.\n   * @param task The task to execute for each input element.\n   * @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).\n   *\n   * @returns An async generator yielding results of the tasks in completion order.\n   */\n  public async *imapUnordered\u003cTInput, TResult\u003e(\n    inputs: Iterable\u003cTInput\u003e | AsyncIterable\u003cTInput\u003e,\n    task: Task\u003cTInput, TResult\u003e,\n    taskHandlers?: TaskHandlers\u003cTInput, TResult\u003e,\n  ): AsyncGenerator\u003cTResult | undefined\u003e;\n\n  /**\n   * Asynchronously processes tasks from the provided inputs in a lazy unordered manner with extended information.\n   * Tasks are executed concurrently using a pool of workers.\n   *\n   * @template TInput The type of the input elements.\n   * @template TResult The type of the result elements.\n   *\n   * @param inputs An iterable or async iterable of input elements.\n   * @param task The task to execute for each input element.\n   * @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).\n   *\n   * @returns An async generator yielding task responses containing the index, result or error for each task.\n   */\n  public async *imapUnorderedExtended\u003cTInput, TResult\u003e(\n    inputs: Iterable\u003cTInput\u003e | AsyncIterable\u003cTInput\u003e,\n    task: Task\u003cTInput, TResult\u003e,\n    taskHandlers?: TaskHandlers\u003cTInput, TResult\u003e,\n  ): AsyncGenerator\u003cTaskResponse\u003cTResult\u003e\u003e;\n\n  /**\n   * Closes the worker pool by terminating all worker processes.\n   * This method should be called when the pool is no longer needed\n   * to ensure that all resources are properly released.\n   */\n  public close();\n}\n```\n\nUnit testing\n------------\n\n```bash\nnpm i\nnpm run test\n```\n\nLicense\n-------\n\nMultiprocessor TS is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Fmultiprocessor-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmoren%2Fmultiprocessor-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Fmultiprocessor-ts/lists"}