1 Star 0 Fork 0

ghosind/node-task-pool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

@antmind/task-pool

Latest version NPM Github Actions build Codacy Badge codecov License

English | 简体中文

@antmind/task-pool是一个简单的Node.js函数任务池实现,实现了对同步、异步方法的并发控制支持。

安装

  • 通过NPM:

    npm install --save @antmind/task-pool
    
  • 通过Yarn:

    yarn add @antmind/task-pool
    

简单使用

  1. 导入@antmind/task-pool包中的TaskPool以及Task

  2. 建立任务池,并根据需求设置并发限制数量(未设置默认为30)。

  3. 创建任务实例并添加到任务池

  4. 调用exec()方法执行任务池中的任务。

示例

import { Task, TaskPool } from '@antmind/task-pool';

const pool = new TaskPool();

for (let i = 5; i > 0; i -= 1) {
  const task = new Task((val: any) => val, i);
  pool.addTask(task);
}

pool.exec().then((data: any) => console.log(data));
// [ 5, 4, 3, 2, 1 ]

并发控制

You can limit the task concurrency number by concurrency option, and this value must equal or great than 0.

在实例化任务池TaskPool时可通过options参数中的concurrency控制并发数,该值必须设置为大于等于0的整数。

import { Task, TaskPool } from '@antmind/task-pool';

const pool = new TaskPool({ concurrency: 3 });

for (let i = 5; i > 0; i -= 1) {
  pool.addTask(
    new Task(
      (val: any) => new Promise((resolve: Function) => {
        setTimeout(
          () => {
            console.log(`num: ${val}`);
            resolve(val);
          },
          val * 100,
        );
      }),
      i,
    ),
  );
}

pool.exec().then((data) => console.log(data));
// num: 3
// num: 4
// num: 5
// num: 1
// num: 2
// [ 5, 4, 3, 2, 1 ]

无限制模式

若将concurrency的值设置为0,则代表无并发数量限制,执行效果等同于Promise.all()

import { Task, TaskPool } from '@antmind/task-pool';

const pool = new TaskPool({ concurrency: 0 });

for (let i = 5; i > 0; i -= 1) {
  pool.addTask(
    new Task(
      (val: any) => new Promise((resolve: Function) => {
        setTimeout(
          () => {
            console.log(`num: ${val}`);
            resolve(val);
          },
          val * 100,
        );
      }),
      i,
    ),
  );
}

pool.exec().then((data) => console.log(data));
// num: 1
// num: 2
// num: 3
// num: 4
// num: 5
// [ 5, 4, 3, 2, 1 ]

配置设置

  • concurrency: 最大并发任务数,需要为大于等于0的整数值,默认值为30。若设置为0则等同于执行Promise.all()

  • throwsError: 当任务执行失败(发生错误)时,是否抛出错误,默认为true。若设置为false,程序将继续执行直到所有任务都执行完成,并保存所有错误信息(可通过getErrors()方法获得。

APIs

TaskPool

构造函数

  • constructor()

  • constructor(options: TaskPoolOptions)

  • constructor(task: Task | Task[], options?: TaskPoolOptions)

类方法

  • exec(): Promise<any[]>

    执行任务池中的任务。

  • addTask(task: Task): number

    将一个任务实例添加到任务池中,并返回任务对应的id。

  • addTasks(task: Task | Task[]): number[]

    将一个或多个任务实例添加到任务池中,并返回任务对应的id。

  • setConcurrency(concurrency: number): void

    设置最大并发数。

  • getErrors(): Promise<Error | undefined>

    获得上次执行产生的错误,错误在数组中的索引对应于任务添加的顺序。

  • getTask(id: number): Task | null

    根据id获得对应的任务。

Task

构造函数

  • constructor(func: Function, ...args: any[])

类方法

  • exec(): any

    执行任务并返回执行结果。

  • setArgs(...args: any[]): void

    重新设置任务参数。

协议

本项目使用MIT协议发布,可查看LICENSE文件获取更多信息。

MIT License Copyright (c) 2021 Chen Su Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A simple Node.js functional tasks pool implementation supported both synchronous and asynchronous functions. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/ghosind/node-task-pool.git
git@gitee.com:ghosind/node-task-pool.git
ghosind
node-task-pool
node-task-pool
main

搜索帮助