好吧,我使用以下代码生成1000万个不同的行,
var serverSeed = 'hidden';
var games = 1e7;
var gamesLeft = games;
generateGames();
function generateGames(){
if(games > 0){
for(var x = 0; x < 1000; x++){
serverSeed = genGameHash(serverSeed);
query('INSERT INTO `hash` SET `hash` = ' + pool.escape(serverSeed) + ', `game` = ' + pool.escape(games-x));
}
games = games - 1000;
console.log("Progress: " + (100 - games/gamesLeft*100).toFixed(2) + "%");
generateGames();
}else{
console.log('Done generating ' + gamesLeft + ' games.');
}
}
虽然从0.00到大约20.00%可以正常但是服务器给我一个错误,基本上告诉我它内存不足。
致命错误:CALL_AND_RETRY_LAST分配失败 - 处理完毕 记忆 中止(核心倾销)
我生成了一个hashchain,所以我需要连续生成它们,我还能做些什么才能生成hashchain?
查询
function query(sql, callback) {
if (typeof callback === 'undefined'){
callback = function() {};
}
pool.getConnection(function(err, connection) {
if(err) return callback(err);
connection.query(sql, function(err, rows) {
if(err) return callback(err);
connection.release();
return callback(null, rows);
});
});
}
答案 0 :(得分:1)
您可能希望进行适当的内存分配并慢慢迭代:
function generateGames(games){
if(!games) return console.log("done.");
var serverSeed = genGameHash(serverSeed);
query('INSERT INTO `hash` SET `hash` = ' + pool.escape(serverSeed) + ', `game` = ' + pool.escape(games),function(){
generateGames(games - 1);
});
}
generateGames(1e7);
这会创建一个插入队列,您可以根据需要添加:
generateGames(1e7/3);
generateGames(1e7/3);
generateGames(1e7/3);
答案 1 :(得分:0)
如果您认为一次可以执行1000次插入,则可以尝试此操作
我使用Promise是因为Promise.all只是让事情变得更容易(也许是因为我以前习惯使用Promises)
const pquery = sql => new Promise((resolve, reject) => query(sql, (err, result) => {
if (err) {
return reject(err);
} else {
resolve(result);
}
}));
var serverSeed = 'hidden';
var totalgames = 1e7;
function generateGames(games){
if (games > 0) {
var batch = Math.min(games, 1000);
Promise.all(Array.from({length:batch}).map((i, x) => {
var serverSeed = genGameHash(serverSeed);
return pquery('INSERT INTO `hash` SET `hash` = ' + pool.escape(serverSeed) + ', `game` = ' + pool.escape(games-x));
})).then(() => {
console.log("Progress: " + (100-(games-1000)/totalgames*100).toFixed(2) + "%");
generateGames(games - 1000);
});
} else {
console.log('done');
}
}
generateGames(totalgames);