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

kne-async-queue

v1.1.0

Published

NodeJs异步队列

Downloads

4

Readme

AsyncQueue

NodeJs 异步(async)队列, 提供一种顺序执行异步任务的机制. 如 web 服务某一个请求的某段代码需要互斥执行时, 就需要将此部分互斥执行代码进行排队

安装

npm install kne-async-queue --save
yarn add kne-async-queue -S

使用

// 示例
const AsyncQueue = require("kne-async-queue");
AsyncQueue.setFileLock(true, './');

const ret = await AsyncQueue.exec("queue name", function() {
	console.log("task code");
}, {
	timeout:0,     // 超时时间, 不超时
	priority: 10,  // 任务优先级
}).catch(err => console.log(err));


let total = 0;
const countTask = async () => {
	let count = total;
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			total = count + 1;
			console.log("total: ", total);
			return resolve(total);
		}, _.random(500, 2000));
	})
}

setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));
setTimeout(() => AsyncQueue.exec("", countTask), _.random(100, 1500));

API

AsyncQueue.create({maxSize, timeout, enableFileLock, fileLockPath})

创建异步队列示例, AsyncQueue 本身也是一个示例.

  • maxSize 队列大小, 默认0
  • timeout 任务超时时间, 默认 0ms
  • enableFileLock 是否开始文件锁 默认 false
  • fileLockPath 文件锁目录 默认 ""
  • lockwait 上锁等待时间 默认 60000ms 超时时间上锁失败, 任务失败

AsyncQueue.exec(key, fn, opt)

执行异步任务

  • key string 队列名称, 具有唯一性
  • fn funtion 任务函数, 必须为函数
  • opt object 可选
    • opt.timeout 超时时间, 默认 0ms
    • opt.priority 任务优先级 默认1000
    • opt.maxSize 当前任务队列最大任务数 队列最大任务数 opt.maxSize || queue.maxSize || AsyncQueue.maxSize

AsyncQueue.setTimeout(key, timeout)

设置指定队列任务超时时间

  • key string|undefined 队列名称, 具有唯一性, 当为 undefined 时设置所有队列默认值, 即 AsyncQueue.timeout = timeout;
  • timeout number 毫秒,

AsyncQueue.setMaxSize(key, maxSize)

设置指定队列大小

  • key string|undefined 队列名称, 具有唯一性, 当为 undefined 时设置所有队列默认值, 即 AsyncQueue.maxSize = maxSize;
  • maxSize number 队列最大任务数 0 不做限制

AsyncQueue.setFileLock(enable, path)

文件锁

  • enable boolean 是否开启文件锁
  • path string 文件锁目录