Search code examples
node.jsnode-async

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 ?


Solution

  • 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.