我正在使用:节点-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));
};