Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
limiting concurrency?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jim Tittsler  
View profile  
 More options Oct 24 2012, 9:37 pm
From: Jim Tittsler <jtitts...@gmail.com>
Date: Wed, 24 Oct 2012 18:37:25 -0700 (PDT)
Local: Wed, Oct 24 2012 9:37 pm
Subject: limiting concurrency?

What is the best pattern for limiting the concurrency of resolving an array
(queue) of promises?  I'd like to have another argument to Q.all (or qq's
queue?) that would allow at most a specified number of the functions being
processed at once.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christoph Dorn  
View profile  
 More options Oct 25 2012, 11:40 am
From: Christoph Dorn <christoph...@christophdorn.com>
Date: Thu, 25 Oct 2012 08:40:00 -0700
Subject: Re: [Q] limiting concurrency?

> Jim Tittsler <mailto:jtitts...@gmail.com>
> 24 October, 2012 6:37 PM
> What is the best pattern for limiting the concurrency of resolving an
> array (queue) of promises?  I'd like to have another argument to Q.all
> (or qq's queue?) that would allow at most a specified number of the
> functions being processed at once.

Here is one solution:
https://github.com/sourcemint/util-js/blob/master/lib/q-throttle.js

Christoph


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Forbes Lindesay  
View profile  
 More options Oct 26 2012, 6:40 am
From: Forbes Lindesay <for...@lindesay.co.uk>
Date: Fri, 26 Oct 2012 03:40:17 -0700 (PDT)
Local: Fri, Oct 26 2012 6:40 am
Subject: Re: limiting concurrency?

What you're asking for doesn't really make sense.  An array of promises
(such as that you'd pass to Q.all) is an array of tasks that have already
been started.  Waiting on them to resolve is, to all intents and purposes,
free.  Waiting on them one at a time, versus in parallel has very little
difference (one has slightly fewer turns of the event loop).  It's when you
call the functions that you begin the tasks, so what you really need to do
is throttle how many times you call the function simultaniously:

You could try something like this:

```
function throttle(functions, max) {
  var def = Q.defer();
  var result = [];
  var running = 0;
  var waitingAt;
  function next(i) {
    if (running > max) {
      waitingAt = i;
    } else if (i < functions.length) {
      var res = functions[i]();
      result.push(res);
      res.then(completeTask, completeTask);
    } else {
      def.resolve(Q.all(result));
    }
  }
  next(0);
  function completeTask() {
    if (waitingAt) {
      next(waitingAt);
      waitingAt = null;
    }
  }
  return def.promise;

}

```

I haven't tested it, but it should take an array of functions and a
max-concurrency value, then execute up to max of them in parallel and
return a promise for an array of the results.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wout Mertens  
View profile  
 More options Nov 23 2012, 10:27 am
From: Wout Mertens <wout.mert...@gmail.com>
Date: Fri, 23 Nov 2012 07:27:41 -0800 (PST)
Local: Fri, Nov 23 2012 10:27 am
Subject: Re: limiting concurrency?

On Thursday, October 25, 2012 3:37:25 AM UTC+2, Jim Tittsler wrote:
> What is the best pattern for limiting the concurrency of resolving an
> array (queue) of promises?  I'd like to have another argument to Q.all (or
> qq's queue?) that would allow at most a specified number of the functions
> being processed at once.

I solved it by using async and creating a queue that promises to give you a
callback to call when you're done.

Coffeescript:
Q = require 'q'
async = require 'async'

promisQue = async.queue(
  (deferred, callback) ->
    deferred.resolve(callback)
  3
)

throttledPromise = () ->
  deferred = Q.defer()
  promisQue.push deferred
  deferred.promise

for i in [1..50]
  ((i) ->
    throttledPromise().then (cb) ->
      setTimeout cb, 500
      console.log i
  )(i)

Javascript:
var Q, async, i, promisQue, throttledPromise, _fn, _i;

Q = require('q');

async = require('async');

promisQue = async.queue(function(deferred, callback) {
  return deferred.resolve(callback);

}, 3);

throttledPromise = function() {
  var deferred;
  deferred = Q.defer();
  promisQue.push(deferred);
  return deferred.promise;

};

_fn = function(i) {
  return throttledPromise().then(function(cb) {
    setTimeout(cb, 500);
    return console.log(i);
  });
};

for (i = _i = 1; _i <= 50; i = ++_i) {
  _fn(i);

}

Thoughts? (the anon (i) function is needed to keep a copy of i for the
promise)

Wout.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wout Mertens  
View profile  
 More options Nov 23 2012, 11:36 am
From: Wout Mertens <wout.mert...@gmail.com>
Date: Fri, 23 Nov 2012 08:36:06 -0800 (PST)
Local: Fri, Nov 23 2012 11:36 am
Subject: Re: limiting concurrency?

On Friday, November 23, 2012 4:27:41 PM UTC+1, Wout Mertens wrote:
> On Thursday, October 25, 2012 3:37:25 AM UTC+2, Jim Tittsler wrote:

>> What is the best pattern for limiting the concurrency of resolving an
>> array (queue) of promises?  I'd like to have another argument to Q.all (or
>> qq's queue?) that would allow at most a specified number of the functions
>> being processed at once.

> I solved it by using async and creating a queue that promises to give you
> a callback to call when you're done.

Now using a class and showing how to inject another promise:

Q = require 'q'
async = require 'async'

class promisQue
  resolver = (deferred, callback) -> deferred.resolve(callback)

  constructor: (num) ->
    @queue = async.queue resolver, num or 3

  P: () ->
    deferred = Q.defer()
    @queue.push deferred
    deferred.promise

q = new promisQue

lastP = null

for i in [1..20]
  ((i) ->
    lastP = q.P().then (cb) ->
      Q.delay(i, 500).then(-> console.log i).finally cb
  )(i)

lastP.then -> console.log "Done!"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »