node-worker-threads-pool-ts
Version:
Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Typescript, Async/Await.
34 lines (33 loc) • 1.48 kB
TypeScript
/// <reference types="node" />
import { Pool } from "./pool";
import { StaticTaskExecutor } from "./task-executor";
import { CommonWorkerSettings } from "./common";
export declare type TaskFuncThis<WorkerData = any> = {
workerData: WorkerData;
require: NodeRequire;
};
export declare type TaskFunc<ParamType, ResultType, WorkerData = any> = ((this: TaskFuncThis<WorkerData>) => Promise<ResultType>) | ((this: TaskFuncThis<WorkerData>) => ResultType) | ((this: TaskFuncThis<WorkerData>, param: ParamType) => Promise<ResultType>) | ((this: TaskFuncThis<WorkerData>, param: ParamType) => ResultType);
export interface StaticPoolOptions<ParamType, ResultType, WorkerData> extends CommonWorkerSettings {
/** number of workers */
size: number;
/** path of worker file or a worker function */
task: string | TaskFunc<ParamType, ResultType>;
/** data to pass into workers */
workerData?: WorkerData;
}
/**
* Threads pool with static task.
*/
export declare class StaticPool<ParamType, ResultType, WorkerData = any> extends Pool {
constructor(opt: StaticPoolOptions<ParamType, ResultType, WorkerData>);
/**
* Choose a idle worker to run the task
* with param provided.
*/
exec(param?: ParamType, timeout?: number): Promise<ResultType>;
/**
* Create a task executor of this pool.
* This is used to apply some advanced settings to a task.
*/
createExecutor(): StaticTaskExecutor<ParamType, ResultType>;
}