pranavk
pranavk

Reputation: 1845

async.parallel for large number of functions

I have a function that I want to call 1 million times. The function makes two calls to the database (first a SELECT and then an UPDATE). My current approach is to store these functions in an array and then call async.parallel on this array.

I am afraid that it would either result in ENOMEM or something.

Any better approach here ?

Upvotes: 2

Views: 1010

Answers (2)

Exos
Exos

Reputation: 3988

You can do a queue with a generator:

var totalTasks = 1000000;
var nTasks = 0;

// Generate data to send to query 
var getData = (function() {
    var i = 0;

    return function() {
        i++;
        return {
            number: i
        };
    };

})();

// Create the task running each time
var task = function(data, done) {
    nTasks++;
    console.log("Make task", nTasks);
    doQuery(data, done);
};

// Create queue with a limit concurrency
var queue = async.queue(task, 10); // <- parallels queries*

// The callback execute each task was execute
var cb = function(err) {
    if (err) throw err;

    // Add new tasks to queue if is neccesary    
    if (nTasks < totalTasks && queue.length() < queue.concurrency) {
        queue.push(getData(), cb);
    }

};

var i;

// Add the first x tasks to queue
for (i = 0; i < queue.concurrency; i++) {
    queue.push(getData(), cb);
}
  • You need to consider the size of the connections pool, or the number of max current proccess of MySQL.

Upvotes: 4

cshion
cshion

Reputation: 1213

if you want to process each row , you could use Mysql stream , so you can apply whatever you want to each row (update in your case);

Upvotes: 0

Related Questions