npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mcjxy/task-pool

v1.1.8

Published

Easy way to manage a pool of workers, support cluster mode(run tasks in different process), thread mode(run tasks in different thread) and normal mode(run tasks in current thread)!

Downloads

14

Readme

task-pool

Easy way to manage a pool of workers, support cluster mode that will run tasks in different process, thread mode that will run tasks in different thread and normal mode that will run tasks in the current thread!

If need to use thread mode, please use the --experimental-worker flag to run correctly, since this resource still experimental in NodeJs.

Introduction

If you want to run tasks in pool, you need this.

Cluster mode and thread mode can help you run out of your computer resource, normal mode can help you mange your tasks.

You can mixed use these mode to get something powerful.

Prerequisites

Installation

$ npm install @mcjxy/task-pool [--save]

Examples

test.js

const TaskPool = require('@mcjxy/task-pool');
const path = require('path');

const taskPool = new TaskPool(9, 200);
(async () => {
  for (let i = 0; i < 200; i++) {
    taskPool.dispatch(path.resolve(__dirname, './task.js'), i).then(v => {
      console.log('main: data ', v);
    }).catch(e => {
      console.log(e);
    });
  }
  await taskPool.wait();

  for (let i = 0; i < 1000; i++) {
    taskPool.dispatch(path.resolve(__dirname, './task.js'), i);
  }
  console.log('start cancel', (new Date()).toISOString());
  await taskPool.cancel();
  console.log('end cancel', (new Date()).toISOString());
})();

Note: In thread mode, if the thread count is more than 10, after you use the console.log in every thread, you will get a warning: (node:9768) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added. Use "emitter.setMaxListeners()" to increase limit. You can add "--trace-warnings" start option to check more information. So be careful of using "console.log" when your thread is more than 9.

Note: In thread mode and cluster mode, each thread or cluster has a separate global data, but tasks in the same thread or cluster can share the global data.

API

TaskPool(workerNum, maxRunningTask, type)

the constructor, after you call this, task pool are ready

  • workerNum : Worker number of the pool. default is cpu number
  • maxRunningTask : Max running tasks of all threads. 0 for unlimited. default is 0
  • type : Type of task pool mode, support 'cluster', 'thread' and 'normal''. default is 'cluster'. 'normal' mode use current thread to run task, you can use this mode to mange the tasks

Note: A running task means that a normal method is not returned or a promise is not triggered.

TaskPool.init(timeout)

wait until pool init end. You don't need to call this function unless you want your task to be executed immediately after call dispatch at the first time

  • timeout : The max time to wait in millisecond. default is infinite.
  • return : Wait until pool init end, or catch the timeout error

TaskPool.dispatch(file, ...args)

dispatch a task, the tasks will add to the queue until any worker can run the task

  • file : <string|function> Javascript absolute file path that will export a function or a function(only normal mode support function type), the function accept two parameter, method(id, ...args), id for the worker id, args for the args input
  • args : A list of args which will be trans to the method of the js file
  • return : <promise> You can use this to get task return data

TaskPool.status()

get the status of the task pool.

  • return : < object > Follow is the key of the object
    • queue : tasks number need to be send to worker,
    • running : tasks number running in all workers,
    • workers : workers count,
    • runningPerWorker : <array[integer]> tasks number running in each worker,
    • runEnd : tasks number run end,
    • runSuccess : tasks number run success,
    • runFailed : tasks number run failed,

TaskPool.wait(timeout)

wait until all the tasks run end

  • timeout : The max time to wait in millisecond. default is infinite.
  • return : Wait tasks run end, or catch the timeout error

TaskPool.waitCanRun(timeout)

wait until running tasks count less than maxRunningTask and queue task count less than maxRunningTask. This is used for avoid out of memory, and get the max effectiveness

  • timeout : The max time to wait in millisecond. default is infinite.
  • return : Wait task can run immediately, or catch the timeout error

Note: If there are a list of waiters , just one waiter can wakeup at one time.

TaskPool.cancel(timeout)

cancel the tasks which are not running, and wait until all the running tasks run end

  • timeout : The max time to wait in millisecond. default is infinite.
  • return : Wait cancel run end, or catch the timeout error

TaskPool.terminate(timeout)

terminate all the workers, thread or cluster will be killed

  • timeout : The max time to wait in millisecond. default is infinite.
  • return : Wait terminate run end, or catch the timeout error

TaskPool.start()

start the task pool workers. You can call TaskPool.init(timeout) function to wait the start end. Unless you call the terminate function, otherwise it will take no effect when you call this function

License

The project is licensed under the MIT License. See the LICENSE file for more details