multiprocessor
Version:
Multiprocessing pool implementation for NodeJS and TypeScript
105 lines (104 loc) • 4.85 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
import type { Task, TaskResponse, TaskHandlers } from "./types";
/**
* A class representing a pool of workers for executing tasks concurrently using child processes.
* Extends EventEmitter to allow task result events to be emitted.
*/
export declare class Pool extends EventEmitter {
/**
* Array of child processes representing the workers in the pool.
*/
private workers;
/**
* Array of child processes representing the available workers in the pool.
*/
private availableWorkers;
/**
* Array of tasks to be processed in the pool.
*/
private taskQueue;
/**
* Map of tasks currently being processed by workers, keyed by worker.
*/
private tasksInProcess;
/**
* Current task index, incremented for each task processed.
*/
private currentTaskIndex;
/**
* Optional handlers for task events (onTaskSuccess, onTaskError).
*/
private taskHandlers;
/**
* Create a new pool with the specified number of workers.
*
* @param poolSize The number of workers to create in the pool.
*/
constructor(poolSize: number);
/**
* Asynchronously processes tasks from the provided inputs in an ordered manner.
* Tasks are executed concurrently using a pool of workers.
*
* @template TInput The type of the input elements.
* @template TResult The type of the result elements.
*
* @param inputs An iterable or async iterable of input elements.
* @param task The task to execute for each input element.
* @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).
*
* @returns A promise that resolves to an array of task results in the order of the input elements.
*/
map<TInput, TResult>(inputs: Iterable<TInput> | AsyncIterable<TInput>, task: Task<TInput, TResult>, taskHandlers?: TaskHandlers<TInput, TResult>): Promise<Array<TResult | undefined>>;
/**
* Asynchronously processes tasks from the provided inputs in a lazy ordered manner.
* Tasks are executed concurrently using a pool of workers.
*
* @template TInput The type of the input elements.
* @template TResult The type of the result elements.
*
* @param inputs An iterable or async iterable of input elements.
* @param task The task to execute for each input element.
* @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).
*
* @returns An async generator yielding results of the tasks in the order of the input elements.
*/
imap<TInput, TResult>(inputs: Iterable<TInput> | AsyncIterable<TInput>, task: Task<TInput, TResult>, taskHandlers?: TaskHandlers<TInput, TResult>): AsyncGenerator<TResult | undefined>;
/**
* Asynchronously processes tasks from the provided inputs in a lazy unordered manner.
* Tasks are executed concurrently using a pool of workers.
*
* @template TInput The type of the input elements.
* @template TResult The type of the result elements.
*
* @param inputs An iterable or async iterable of input elements.
* @param task The task to execute for each input element.
* @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).
*
* @returns An async generator yielding results of the tasks in completion order.
*/
imapUnordered<TInput, TResult>(inputs: Iterable<TInput> | AsyncIterable<TInput>, task: Task<TInput, TResult>, taskHandlers?: TaskHandlers<TInput, TResult>): AsyncGenerator<TResult | undefined>;
/**
* Asynchronously processes tasks from the provided inputs in a lazy unordered manner with extended information.
* Tasks are executed concurrently using a pool of workers.
*
* @template TInput The type of the input elements.
* @template TResult The type of the result elements.
*
* @param inputs An iterable or async iterable of input elements.
* @param task The task to execute for each input element.
* @param taskHandlers Optional handlers for task events (onTaskSuccess, onTaskError).
*
* @returns An async generator yielding task responses containing the index, result or error for each task.
*/
imapUnorderedExtended<TInput, TResult>(inputs: Iterable<TInput> | AsyncIterable<TInput>, task: Task<TInput, TResult>, taskHandlers?: TaskHandlers<TInput, TResult>): AsyncGenerator<TaskResponse<TResult>>;
/**
* Closes the worker pool by terminating all worker processes.
* This method should be called when the pool is no longer needed
* to ensure that all resources are properly released.
*/
close(): void;
private processQueue;
private initWorkers;
private createEmptyHandler;
}