New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

thread-pool-ts

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thread-pool-ts

基于 worker_threads 的通用线程池任务执行器(支持并发控制、顺序输出、超时终止)

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

🧵 ThreadPool - Node.js 多线程任务池

基于 worker_threads 封装的轻量级线程池,支持:

  • 多任务并发执行(限制最大并发)
  • 支持顺序回调(按任务 index 顺序)
  • 支持任务超时控制
  • 支持任务完成后的钩子函数(onComplete)
  • 任务结果收集(可选)

📦 安装

npm install thread-pool-ts
# or
pnpm add thread-pool-ts

⚠️ 仅支持 Node.js 12+,不支持浏览器环境。

✨ 快速使用

import { ThreadPool } from "thread-pool-ts";

const pool = new ThreadPool({
  workerPath: "./worker.js", // Worker 文件路径
  maxConcurrency: 4, // 最大并发数
  isSequential: true, // 是否按顺序执行 onResult
  timeout: 5000, // 每个任务超时时间(毫秒)
});

const tasks = ["任务1", "任务2", "任务3"];

pool.run<string, string>({
  tasks,
  onResult: (res, i) => {
    console.log(`第 ${i} 个结果:`, res);
  },
  onComplete: (allResults) => {
    console.log("所有任务完成:", allResults);
  },
});

npm version

Keywords

thread

FAQs

Package last updated on 18 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts