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

@axetroy/flow

v0.1.0

Published

concurrency flow controller.

Downloads

1

Readme

flow

用于多个异步并发的控制器, 能够控制并发数量.

当前面的任务执行完成之后, 会立刻执行下一个任务. 但是保证并发的数量不变

适用场景

假设有无数个要完成的异步任务,要跑完这些任务但又不是马上, 而是分批次依次完成.

经典场景:爬虫

假如要爬一千万条商品信息. 如果要直接发送一千万个http请求, 服务器直接炸裂. 或者还没有请求那么多次,就已经被服务器ban掉.

所以才写了这个库. 按批次依次爬取.

实现如上需求的伪代码:

const itemList = [1, 2, 3, 4, 10000000];

const flow = new Flow(10);    // 每次并发10个任务, 而且往后正在运行的任务也总是10个.

itemList.forEach(function (item) {
  flow.append(function (next) {   // 循环添加任务到队列里面
    http.get(`http://example.com/item/${item}`)
      .then(function (response) {
        // 爬取数据成功, 做你改做的事吧
        next();  // 进入到下一个任务
      })
      .catch(function (err) {
        console.error(err);
        next();
      })
  });
});

flow.run()
  .then(function () {
    console.log('Tasks done');
  });

安装

npm install @axetroy/flow

或者如果你正在使用 yarn(推荐)

yarn add @axetroy/flow

使用


function task1(next) {
  setTimeout(
    function() {
      console.log('task1');
      next();
    },
    1000
  );
}

function task2(next) {
  setTimeout(
    function() {
      console.log('task2');
      next();
    },
    900
  );
}

function task3(next) {
  setTimeout(
    function() {
      console.log('task3');
      next();
    },
    800
  );
}

function task4(next) {
  setTimeout(
    function() {
      console.log('task4');
      next();
    },
    700
  );
}

function task5(next) {
  setTimeout(
    function() {
      console.log('task5');
      next();
    },
    600
  );
}

function task6(next) {
  setTimeout(
    function() {
      console.log('task6');
      next();
    },
    500
  );
}

// 假设最多只能并发2个任务
const flow = new Flow(2);

flow
    .append(task1)
    .append(task2)
    .append(task3)
    .append(task4)
    .append(task5)
    .append(task6)
    .run();

/*
print out


task2
task1
task4
task3
task6
task5
*/

API

.append(task:Function): this

append task to the flow.

.run(): Promise

run start

测试

git clone https://github.com/axetroy/flow.git
cd ./flow.js
yarn
yarn run test

参与贡献

git clone https://github.com/axetroy/flow.git
cd ./flow.js
yarn
yarn run test

You can flow Contribute Guide

贡献者

| Axetroy💻 🔌 ⚠️ 🐛 🎨 | | :---: |

开源许可

The MIT License