将JS函数添加到并行队列中
Adding JS functions to a parallel queue
我有这段代码,我想运行所有的数据库插入,然后在脚本结束时断开与数据库的连接。问题是,我需要在循环结束后执行断开连接回调,但这是不确定的。因此,我需要为每个循环创建一个函数,然后只有在所有这些函数完成后,才能调用该函数以断开与mongoose/mongoDB的连接。
有人看到我的代码有问题吗?
//这段代码很接近,但还不完全,因为在进行多次插入/保存之前,它将与mongo断开连接
var mongoose = require('mongoose')
, Admin = mongoose.mongo.Admin;
var UserModel = require('../models/UserModel');
UserModel.registerSchema(mongoose);
var fs = require('fs');
var parsedJSON = JSON.parse(fs.readFileSync('../dummy_data/dummy_user_data', 'utf8'));
var system_db = mongoose.connect('mongodb://localhost:27017/local_dev_db');
function insertUsers(callback){
parsedJSON.forEach(function (item, index) {
var User = UserModel.getNewUser(system_db);
var user = new User({
username: item.username,
password: item.password,
address: item.address,
phone: item.phone,
email: item.email,
gender: item.gender,
about: item.about,
latitude: item.latitude,
longitude: item.longitude
});
user.save(function (err, result) {
if (err) {
console.log("error in player save method:", err);
}
console.log(index);
if (result) {
//console.log('Added!', result);
}
});
});
callback();
}
function disconnect(){
mongoose.disconnect();
mongoose.connection
.close(function () {
console
.log('Mongoose connection disconnected');
process.exit(0);
});
}
insertUsers(disconnect);
因此,解决方案是使用async.parallel库,并且只有在完成所有函数之后,才调用最终的回调函数。然而,我如何通过编程来实现这一点,而不是将一组已知的函数放入async.pallel代码块,我需要将未知数量的函数放入aysnc.parallel码块。你看见了吗?
在这种特殊的情况下,async.each()实际上比async.palallel()更适合。async.ech()是并行的,而async.echSeries()不是。这是代码:
function insertUsers(callback){
var User = UserModel.getNewUser(system_db);
async.each(parsedJSON, function (item, eachCb) {
var user = new User({
username: item.username,
password: item.password,
address: item.address,
phone: item.phone,
email: item.email,
gender: item.gender,
about: item.about,
latitude: item.latitude,
longitude: item.longitude
});
user.save(function (err, result) {
if (err) {
console.log("error in player save method:", err);
}
console.log(index);
if (result) {
//console.log('Added!', result);
}
eachCb(); // if calling with eachCb(err), async.each()
// will not continue the rest of the items
// in case of error occurs
});
}, function(err) {
callback(); // done with all user.save() calls
});
}
当然可以。只需将函数动态添加到将传递给async.parallel
:的数组中
var fs = require('fs');
var async = require('async');
var mongoose = require('mongoose'),
Admin = mongoose.mongo.Admin;
var UserModel = require('../models/UserModel');
UserModel.registerSchema(mongoose);
var parsedJSON = JSON.parse(fs.readFileSync('../dummy_data/dummy_user_data', 'utf8'));
var system_db = mongoose.connect('mongodb://localhost:27017/local_dev_db');
var insertFunctions = [];
parsedJSON.forEach(function (item, index) {
insertFunctions.push(function(callback) {
var User = UserModel.getNewUser(system_db);
var user = new User({
username: item.username,
password: item.password,
address: item.address,
phone: item.phone,
email: item.email,
gender: item.gender,
about: item.about,
latitude: item.latitude,
longitude: item.longitude
});
user.save(function (err, result) {
if (err) {
console.log("error in player save method:", err);
callback(err);
return;
}
callback(null, result);
});
});
});
function disconnect() {
mongoose.disconnect();
mongoose.connection
.close(function () {
console
.log('Mongoose connection disconnected');
process.exit(0);
});
}
//First parameter is the array of functions to run in parallel,
// second parameter is the callback function
async.parallel(insertFunctions, disconnect);
相关文章:
- Javascript/jQuery中的并行Ajax调用
- 如何处理node.js节点mongodb中的连接和查询队列
- 异步并行错误
- 节点协同与生成器和Promise并行流量控制
- 如何使用JS/nightwatchjs并行运行多个测试
- Mongodb数千个并行运行的查询
- 事件循环、回调队列和 Javascript 的单线程是如何连接的
- 如何使用队列呈现多个HighCharts以防止加载时间过长
- 起始节点.js用于并行的线程池
- 使用appendChild()动态添加的脚本是按顺序执行还是并行执行
- Angular 2中Observable中获取数据后的队列/回调函数
- 阻止AJAX队列阻止浏览器
- 将JS函数添加到并行队列中
- 如何在uploadify/uploadifive中使用OnQueueComplete事件在队列完成时发送电子邮件
- 使用RxJS模拟命令队列和撤消堆栈
- 使用jquery队列按顺序淡入任意数量的文本行
- jquery队列自动搜索现有用户
- 如何在并行选项卡中运行彗星聊天
- 更改 jQuery 队列中动画的缓动函数
- 突然停止功能,删除队列