我正在使用:node-mysql
arrtickers得到了大约200个值。arrtickers闭包将值传入sym。
sym会将值传递给每个函数(在异步模式下运行,每个fn将自行开始运行,而不必等待之前的fn完成)
这里的问题是mysql似乎无法处理多个调用?events.js:48
throw arguments[1]; // Unhandled 'error' event
^
Error: reconnection attempt failed before connection was fully set up
at Socket.<anonymous> (/home/ubuntu/node/node_modules/mysql/lib/client.js:67:28)
at Socket.emit (events.js:64:17)
at TCP.onread (net.js:398:51)
arrtickers.forEach(function(value) {
var sym= value;
(function(sym) {
url2= "http://test2.com/quote.ashx?t="+sym+"&ty=c&ta=1&p=d&b=1";
request({ uri:url2 }, function (error, response, body) {
jsdom.env({
html: body,
scripts: [
jqlib
]
}, function (err, window) {
var $ = window.jQuery;
var data= $('body').html();
//some scrapping
var client2 = mysql.createClient({
user: 'root',
password: '123',
host: '127.0.0.1',
port: '3306'
});
client2.query('USE testtable');
sql= "update tbla SET a='"+a+"', b='"+b+"', c='"+c+"', d='"+d+"' where ticker='"+sym+"'";
client2.query(
sql, function(err, info){
if (err) {
throw err;
}
}
);
client2.end();
});
});
})(sym);
(function(sym) {
url= "http://test3.com/quote.ashx?t="+sym+"&ty=c&ta=1&p=d&b=1";
request({ uri:url3 }, function (error, response, body) {
jsdom.env({
html: body,
scripts: [
jqlib
]
}, function (err, window) {
var $ = window.jQuery;
var data= $('body').html();
//some scrapping
var client3 = mysql.createClient({
user: 'root',
password: '123',
host: '127.0.0.1',
port: '3306'
});
client3.query('USE testtable');
sql= "update tbla SET a='"+a+"', b='"+b+"', c='"+c+"', d='"+d+"' where ticker='"+sym+"'";
client3.query(
sql, function(err, info){
if (err) {
throw err;
}
}
);
client3.end();
});
});
})(sym);
(function(sym) {
url= "http://test4.com/quote.ashx?t="+sym+"&ty=c&ta=1&p=d&b=1";
request({ uri:url4 }, function (error, response, body) {
jsdom.env({
html: body,
scripts: [
jqlib
]
}, function (err, window) {
var $ = window.jQuery;
var data= $('body').html();
//some scrapping
var client4 = mysql.createClient({
user: 'root',
password: '123',
host: '127.0.0.1',
port: '3306'
});
client4.query('USE testtable');
sql= "update tbla SET a='"+a+"', b='"+b+"', c='"+c+"', d='"+d+"' where ticker='"+sym+"'";
client4.query(
sql, function(err, info){
if (err) {
throw err;
}
}
);
client4.end();
});
});
})(sym);
//same function repeat for test5.com, test6.com, test7.com, test8.com, test9.com
});
以下是来自client.js的代码的一部分(node-mysql的一部分) 我真的不明白整个过程如何联系在一起,任何想法的人?
Client.prototype._connect = function() {
this.destroy();
var socket = this._socket = new Socket();
var parser = this._parser = new Parser();
var self = this;
socket
.on('error', this._connectionErrorHandler())
.on('data', parser.write.bind(parser))
.on('end', function() {
if (self.ending) {
// @todo destroy()?
self.connected = false;
self.ending = false;
if (self._queue.length) {
self._connect();
}
return;
}
if (!self.connected) {
this.emit('error', new Error('reconnection attempt failed before connection was fully set up'));
return;
}
self._connect();
})
.connect(this.port, this.host);
parser.on('packet', this._handlePacket.bind(this));
};
答案 0 :(得分:1)
您可以使用node-mysql-queues模块,它是您正在使用的当前模块的包装器。以下是信息:https://github.com/bminer/node-mysql-queues
从其页面中查看多个查询示例:
var q = client.createQueue();
q.query(...);
q.query(...);
q.execute();
client.query(...); //Will not execute until all queued queries (and their callbacks) completed.