nodejs队列
nodejs队列
创建具有指定并发性的队列对象。添加到队列的任务以并行方式处理(直到并发性限制)。如果所有的worker都在进行中,任务就会排队,直到有一个worker可用。worker完成任务后,将调用该任务的回调。
priorityQueue对象,queue和priorityQueue对象有两个区别: push(任务,优先级,[回调])-优先级应该是一个数字。如果给定了一组任务,则所有任务将被分配相同的优先级。没有unshift 。
// create a queue object with concurrency 2
var q = async.queue(function(task, callback) {
console.log('hello ' + task.name);
callback();
}, 2);
// assign a callback
q.drain(function() {
console.log('all items have been processed');
});
// or await the end
// await q.drain()
// assign an error callback
q.error(function(err, task) {
console.error('task experienced an error');
});
// add some items to the queue
q.push({name: 'foo'}, function(err) {
console.log('finished processing foo');
});
// callback is optional
q.push({name: 'bar'});
// add some items to the queue (batch-wise)
q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function(err) {
console.log('finished processing item');
});
// add some items to the front of the queue
q.unshift({name: 'bar'}, function (err) {
console.log('finished processing bar');
});
//--------------------------
// create a queue object with concurrency 1
var q = async.priorityQueue(function(task, callback) {
console.log('Hello ' + task.name);
callback();
}, 1);
// assign a callback
q.drain = function() {
console.log('All items have been processed');
};
// add some items to the queue with priority
q.push({name: 'foo3'}, 3, function(err) {
console.log('Finished processing foo');
});
q.push({name: 'bar2'}, 2, function (err) {
console.log('Finished processing bar');
});
// add some items to the queue (batch-wise) which will have same priority
q.push([{name: 'baz1'},{name: 'bay1'},{name: 'bax1'}], 1, function(err) {
console.log('Finished processing item');
});
输出结果如下:
hello bar
finished processing bar
hello foo
finished processing foo
hello bar
hello baz
finished processing item
hello bay
finished processing item
hello bax
finished processing item
all items have been processed
Hello bay1
Finished processing item
Hello bax1
Finished processing item
Hello bar2
Finished processing bar
Hello foo3
Finished processing foo
参考:
https://github.com/caolan/async/blob/v1.5.2/README.md
https://medium.com/velotio-perspectives/understanding-node-js-async-flows-parallel-serial-waterfall-and-queues-6f9c4badbc17
nodejs队列的更多相关文章
- NodeJS系列~第三个小例子,NodeJs与Redis实现高并发的队列存储
返回目录 众所周知 redis量个强大的缓存组件,可以部署在win32和linux环境之上,它有五大存储结构,其中有一种为列表list,它可以实现quene和stack的功能,即队列和堆栈的功能. r ...
- queue-fun —— nodejs下基于Promise的队列控制模块。
工作告一段落,闲来无事,写了一个在nodejs实现“半阻塞”的控制程序. 一直以来,nodejs以单线程非阻塞,高并发的特性而闻名.搞这个“半阻塞”是东西,有什么用呢? 场景一: 现在的web应用可有 ...
- 用NodeJs实现优先级队列PQueue
优先级队列(PriorityQueue)是个很有用的数据结构,很多编程语言都有实现.NodeJs是一个比较新潮的服务器语言,貌似还没有提供相关类.这些天有用到优先级队列,因为时间很充足,闲来无事,就自 ...
- 使用NODEJS+REDIS开发一个消息队列以及定时任务处理
作者:RobanLee 原创文章,转载请注明: 萝卜李 http://www.robanlee.com 源码在这里: https://github.com/robanlee123/RobCron 时间 ...
- NodeJs之定时器与队列
NodeJs之定时器与队列 一,介绍与需求 1.1,介绍 定时任务(node-schedule),是针对Node.js的一种灵活的cron-like和not-cron-like作业调度程序.它允许您使 ...
- 架构设计之NodeJS操作消息队列RabbitMQ
一. 什么是消息队列? 消息(Message)是指在应用间传送的数据.消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象. 消息队列(Message Queue)是一种应用间的通信 ...
- nodejs一个函数实现消息队列中间件
消息队列中间件(Message Queue)相信大家不会陌生,如Kafka.RabbitMQ.RocketMQ等,已经非常成熟,在大大小小的公司和项目中也已经广泛使用. 有些项目中,如果是只使用初步的 ...
- 搭建前端监控系统(五)Nodejs怎么搭建消息队列
怎样定位前端线上问题,一直以来,都是很头疼的问题,因为它发生于用户的一系列操作之后.错误的原因可能源于机型,网络环境,接口请求,复杂的操作行为等等,在我们想要去解决的时候很难复现出来,自然也就无法解决 ...
- Nodejs事件引擎libuv源码剖析之:高效队列(queue)的实现
声明:本文为原创博文,转载请注明出处. 在libuv中,有一个只使用简单的宏封装成的高效队列(queue),现在我们就来看一下它是怎么实现的. 首先,看一下queue中最基本的几个宏: typede ...
- 如何使用NODEJS+REDIS开发一个消息队列
作者: RobanLee 原创文章,转载请注明: 萝卜李 http://www.robanlee.com MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应 ...
随机推荐
- Docker网络:Docker0、容器互联技术--link、自定义网络、实战部署Redis集群
一.Docker网络 ● --理解Docker0 在干净的Linux环境上安装docker(将docker 的所有镜像.容器先删除,干干净净!)实验: 1.查看本地网络信息 ip addr 可见有三个 ...
- 原生js元素拖动效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C 将十进制数转换成二~十六进制数中的任意一种
问题:将一个十进制整数转换成二~十六进制数中的任意一种进制数 代码: #include <stdio.h> #include <stdlib.h> int b; int i = ...
- 平滑升级mariadb
问题 Centos7自带的MariaDB版本是5.5 ,版本过于老旧,现想升级到最新版本,且数据不丢失 措施 备份原来的数据 mysqldump -u root -p --all-databases ...
- 重试机制与 CompletableFuture 拓展
重试机制与 CompletableFuture 拓展 禁止转载. 本文旨在讨论重试机制的特点和策略,分析常用重试类库的实现,讨论为 CompletableFuture 添加重试机制的方法.文章首发同名 ...
- 使用天翼云云容器引擎CCE创建简单nginx服务
本文分享自天翼云开发者社区<使用天翼云云容器引擎CCE创建简单nginx服务>,作者:b****n 一.创建一个nginx应用. 1.选择资源池,如[杭州2],进入云容器引擎CCE平台页面 ...
- Project Euler 728 题解
Problem 728 Circle of Coins 得到 Wallbreaker5th 的指导. \(F\) 就是求这些环上区间(记为 \(A\))的异或线性基大小.令 \(A'_i\gets A ...
- DotNet跨平台 - docker+nginx+ssl 负载均衡
环境:CentOS7 服务器需要安装:docker.nginx.OpenSSL 一.部署方案 在linux服务器上,我们的Web站点程序采用docker容器部署,为了演示负载均衡,我们在同一台linu ...
- WPF DevExpress GridColumn ComboBox 显示选择内容的 TooTip
实现显示当前选择的ComboBox中项的ToolTip信息: 1. 设置 GridColumn 的 CellTemplate 为 ComboBoxEdit , 然后自定义他的 ItemContaine ...
- (抄自己luogu上的博客)莫队总结
虽然当时文风很2,但是觉得写的蛮好的,就在这里贴一下吧. 最近学了分块(太难想了 \(qwq\) )和莫队(太神奇了 \(0w0\) ),写一个阶段性总结~ 分块 总所周知,分块是一种神奇的暴力,用 ...