Lia*_*iam 7 javascript couchdb node.js express
我有一个应用程序,我正在Node.js编写,需要进行大量的配置和数据库调用,以处理用户数据.我遇到的问题是,在11,800多个函数调用之后,Node会抛出一个错误并退出进程.
错误说明:RangeError:超出最大调用堆栈大小
我很好奇是否有其他人已经出现这种情况,并知道他们是如何处理这个问题的.我已经开始将我的代码分解为几个额外的工作文件,但即便如此,每次处理数据节点时,它都需要触摸2个数据库(最多25次调用以更新各种表)并进行一些清理检查.
我完全愿意承认,如果是这种情况,我可能会做一些非最佳的事情,但如果有更优化的方式,我会很感激.
以下是我在数据上运行的代码示例:
app.post('/initspeaker', function(req, res) {
// if the Admin ID is not present ignore
if(req.body.xyzid!=config.adminid) {
res.send( {} );
return;
}
var gcnt = 0, dbsize = 0, goutput = [], goutputdata = [], xyzuserdataCallers = [];
xyz.loadbatchfile( xyz.getbatchurl("speakers", "csv"), function(data) {
var parsed = csv.parse(data);
console.log("lexicon", parsed[0]);
for(var i=1;i<parsed.length;i++) {
if(typeof parsed[i][0] != 'undefined' && parsed[i][0]!='name') {
var xyzevent = require('./lib/model/xyz_speaker').create(parsed[i], parsed[0]);
xyzevent.isPresenter = true;
goutput.push(xyzevent);
}
}
dbsize = goutput.length;
xyzuserdataCallers = [new xyzuserdata(),
new xyzuserdata(),
new xyzuserdata(),
new xyzuserdata(),
new xyzuserdata(),
new xyzuserdata(),
new xyzuserdata(),
new xyzuserdata()
];
// insert all Scheduled Items into the DB
xyzuserdataCallers[0].sendSpeakerData(goutput[0]);
for(var i=1;i<xyzuserdataCallers;i++) {
xyzuserdataCallers[i].sendSpeakerData(8008);
}
//sendSpeakerData(goutput[0]);
});
var callback = function(data, func) {
//console.log(data);
if(data && data!=8008) {
if(gcnt>=dbsize) {
res.send("done");
} else {
gcnt++;
func.sendSpeakerData(goutput[gcnt]);
}
} else {
gcnt++;
func.sendSpeakerData(goutput[gcnt]);
}
};
// callback loop for fetching registrants for events from SMW
var xyzuserdata = function() {};
xyzuserdata.prototype.sendSpeakerData = function(data) {
var thisfunc = this;
if(data && data!=8008) {
//console.log('creating user from data', gcnt, dbsize);
var userdata = require('./lib/model/user').create(data.toObject());
var speakerdata = userdata.toObject();
speakerdata.uid = uuid.v1();
speakerdata.isPresenter = true;
couchdb.insert(speakerdata, config.couch.db.user, function($data) {
if($data==false) {
// if this fails it is probably due to a UID colliding
console.log("*** trying user data again ***");
speakerdata.uid = uuid.v1();
arguments.callee( speakerdata );
} else {
callback($data, thisfunc);
}
});
} else {
gcnt++;
arguments.callee(goutput[gcnt]);
}
};
});
Run Code Online (Sandbox Code Playgroud)
这里定义了几个类和项目,需要一些介绍:
此代码在应用程序中有效,但过了一段时间后,我收到一条错误消息,说已经进行了超过11,800次调用并且应用程序中断了.这不是包含堆栈跟踪的错误,如果它是代码错误,它会因为调用的次数而退出.
再次,任何协助/评论/指示将不胜感激.
它看起来像递归使用xyzuserdata.sendSpeakerData和回调,以保持DB调用顺序.在某些时候你用完了电话堆栈......
Flow-JS 甚至还有一个便利功能,可以在数组元素上串行应用函数:
flow.serialForEach(goutput, xyzuserdata.sendSpeakerData, ...)
Run Code Online (Sandbox Code Playgroud)
我使用flow.serialForEach编写了一个小测试程序,但不幸的是能够得到一个Maximum call stack size exceeded错误 - 看起来像Flow-JS以类似的方式使用调用堆栈来保持同步.
另一种不构建调用堆栈的方法是避免递归并使用超时值为0的setTimeout来调度回调调用.见 http://metaduck.com/post/2675027550/asynchronous-iteration-patterns-in-node-js
您可以尝试使用替换回调调用
setTimeout(callback, 0, [$data, thisfunc])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3680 次 |
| 最近记录: |