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

controlled-concurrency

v1.0.0

Published

Library for controlled parallel execution of async functions, with no external dependencies

Downloads

5

Readme

controlled-concurrency

Build status npm version License Bettercodehub rating

controlled-concurrency is a library, implemented in TypeScript, to execute promises in parallel with a limited concurrency.

It does not require pre-building a list of all promises but instead will accept promises on-the-fly. Promises are processed as soon as they are added until the maxRunning threshold is reached. When the threshold is reached, the main thread execution will wait until a slot gets freed to process the new promise.

The typical use case for ParallelizeBasic is when you want to spawn a controlled amount of processes, each running a somewhat long task (due to the way the queue is handled, there is significant overhead when tasks are shorter than ~10ms), without caring about their output in the main process.

The Parallelize class offers more options. In particular, you can choose to swallow or forward thrown exceptions, by setting throwOnError to false or true, but the latter is currently not very well supported. You can also chose to collect results, by setting storeResults to true.

Installation

npm i -S controlled-concurrency

Usage

import parallelize from 'controlled-concurrency';

function wait(seconds: number): Promise<void> {
  return new Promise((resolve, reject) => {
    if (seconds <= 0) resolve();
    else setTimeout(() => {console.log('tick'); resolve()}, seconds * 1000);
  });
}

async function main(): Promise<void> {
  console.log('Running full Parallelize');
  const parallel = new parallelize.Parallelize({
    maxRunning: 3,
    throwOnError: false,
    storeResults: true,
    granularity: 0.001
  });
  for (let i = 0; i < 5; i++) {
    await parallel.run(wait(1));
  }
  const result = await parallel.finish();
  console.log(result);

  await main2();
}

async function main2(): Promise<void> {
  console.log('Running ParallelizeBasic');
  const parallelBasic = new parallelize.ParallelizeBasic(3);
  for (let i = 0; i < 5; i++) {
    await parallelBasic.run(wait(1));
  }
  await parallelBasic.finish();
}

main()
.then(() => console.log('ok'));

Options

ParallelizeBasic only take a single and mandatory option, the number of threads, passed as a number to the constructor: new parallelize.ParallelizeBasic(3)

Parallelize takes the following options

  • maxRunning: number, mandatory, max number of threads.
  • throwOnError: boolean, optional (default false),, whether or not exceptions thrown by running processes should be forwarded. Support for true is still a bit messy at the moment so you may want to set it to false and catch them yourself, like await parallel.run(wait(0.1).catch(e => {do something})), or just use storeResults
  • storeResults: boolean, optional (default false), whether or not to collect the results of the promises. If true, the results can be collected from finish(). They will be in a format similar to Promise.allSettled(), and sorted in the same order as the promises were added to the queue.
  • granularity: number, optional (default 0.001), seconds between each check for an empty spot in the execution pool, when the pool is full. This is the reason why this library is not appropriate for very very short tasks.

ParallelizeBasic is similar to running Parallelize with throwOnError = false, storeResults = false, and granularity = 0.001 (but uses different, more concise code).

Contributing

Global dev dependencies:

npm i -g typescript
npm i -g chai mocha ts-mocha
npm i -g nyc
npm i -g eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser

Compiling, testing, coverage

tsc -w
npm run build
npm test
npm run coverage

License

This project is licensed under the GNU Lesser General Public License v3 or later - see the license.md file for details