Skip to content Skip to sidebar Skip to footer

How Can I Limit Q Promise Concurrency?

How do I write a method that limits Q promise concurrency? For instance, I have a method spawnProcess. It returns a Q promise. I want no more than 5 process spawned at a time, but

Solution 1:

I have a library that does this for you https://github.com/ForbesLindesay/throat

You can use it via browserify or download the standalone build from brcdn (https://www.brcdn.org/?module=throat&version=latest) and add it as a script tag.

Then (assuming the Promise constructor is polyfilled or implemented in your environment) you can do:

//remove this line if using standalone buildvar throat = require('throat');

functionlimitConcurrency(promiseFactory, limit) {
  var fn = throat(promiseFactory, limit);
  returnfunction () {
    returnQ(fn.apply(this, arguments));
  }
}

You could just call throat(promiseFactory, limit) directly but that would return a promise promise rather than a Q promise.

I also really like using it with array.map.

// only allow 3 parallel downloadsvar downloadedItems = Q.all(items.map(throat(download, 3)));

Solution 2:

This seems to be working for me.

I'm not sure if I could simplify it. The recursion in scheduleNextJob is necessary so the running < limit and limit++ always execute in the same tick.

Also available as a gist.

'use strict';

var Q = require('q');

/**
 * Constructs a function that proxies to promiseFactory
 * limiting the count of promises that can run simultaneously.
 * @param promiseFactory function that returns promises.
 * @param limit how many promises are allowed to be running at the same time.
 * @returns function that returns a promise that eventually proxies to promiseFactory.
 */functionlimitConcurrency(promiseFactory, limit) {
  var running = 0,
      semaphore;

  functionscheduleNextJob() {
    if (running < limit) {
      running++;
      returnQ();
    }

    if (!semaphore) {
      semaphore = Q.defer();
    }

    return semaphore.promise
      .finally(scheduleNextJob);
  }

  functionprocessScheduledJobs() {
    running--;

    if (semaphore && running < limit) {
      semaphore.resolve();
      semaphore = null;
    }
  }

  returnfunction () {
    var args = arguments;

    functionrunJob() {
      return promiseFactory.apply(this, args);
    }

    returnscheduleNextJob()
      .then(runJob)
      .finally(processScheduledJobs);
  };
}

module.exports = {
  limitConcurrency: limitConcurrency
}

Solution 3:

The Deferred promise implementation has gate function which works exactly that way:

spawnProcess = deferred.gate(spawnProcess, 5);    

Solution 4:

I wrote a little library to do this: https://github.com/suprememoocow/qlimit

It's extremely easy to use and is specifically designed to work with Q promises:

var qlimit = require('qlimit');
var limit = qlimit(2); // 2 being the maximum concurrency// Using the same example as abovereturn Q.all(items.map(limit(function(item, index, collection) { 
  returnperformOperationOnItem(item);
}));

It can also be used to limit concurrency to a specific resource, like this:

var qlimit = require('qlimit');
var limit = qlimit(2); // 2 being the maximum concurrencyvar fetchSomethingFromEasilyOverwhelmedBackendServer = limit(function(id) {
  // Emulating the backend servicereturn Q.delay(1000)
    .thenResolve({ hello: 'world' }); 
});

Post a Comment for "How Can I Limit Q Promise Concurrency?"