Worker 池大小,默认为 4
配置选项
返回一个 Worker 池任务函数
const workerPool = createWorkerPool(
(data: number[]) => data.reduce((sum, n) => sum + n, 0),
4 // 4 个 worker
);
// 并发执行多个任务
const tasks = [
workerPool([1, 2, 3]),
workerPool([4, 5, 6]),
workerPool([7, 8, 9]),
workerPool([10, 11, 12])
];
const results = await Promise.all(tasks);
// 清理资源
workerPool.terminate();
要在 Worker 中运行的(异步)函数