限制在给定时间打开的Promise数量

IT技术 javascript typescript promise throttling
2021-01-18 03:15:37

以下 TypeScript 一次执行一次调用doSomething(action)(意味着列表中的第二个项目在第一个完成之前不会被调用)。

async performActionsOneAtATime() {
    for (let action of listOfActions) {
        const actionResult = await doSomethingOnServer(action);
        console.log(`Action Done: ${actionResult}`);
    }
 }

这将立即将所有请求发送到服务器(无需等待任何响应):

async performActionsInParallel() {
    for (let action of listOfActions) {
        const actionResultPromise = doSomething(action);
        actionResultPromise.then((actionResult) => {
            console.log(`Action Done: ${actionResult}`);
        });
    }
}

但我真正需要的是一种抑制它们的方法。可能一次打开 10 或 20 个电话。(一次一个太慢了,但所有 600 个都会使服务器过载。)

但我很难弄清楚这一点。

关于如何限制一次打开 X 的调用次数有什么建议吗?

(这个问题使用 TypeScript,但我会用 ES6 JavaScript 回答。)

6个回答

您可以在一个简短的函数中完成此操作。更新:根据 naomik 的建议按顺序返回值。)

/**
 * Performs a list of callable actions (promise factories) so that only a limited
 * number of promises are pending at any given time.
 *
 * @param listOfCallableActions An array of callable functions, which should
 *     return promises.
 * @param limit The maximum number of promises to have pending at once.
 * @returns A Promise that resolves to the full list of values when everything is done.
 */
function throttleActions(listOfCallableActions, limit) {
  // We'll need to store which is the next promise in the list.
  let i = 0;
  let resultArray = new Array(listOfCallableActions.length);

  // Now define what happens when any of the actions completes. Javascript is
  // (mostly) single-threaded, so only one completion handler will call at a
  // given time. Because we return doNextAction, the Promise chain continues as
  // long as there's an action left in the list.
  function doNextAction() {
    if (i < listOfCallableActions.length) {
      // Save the current value of i, so we can put the result in the right place
      let actionIndex = i++;
      let nextAction = listOfCallableActions[actionIndex];
      return Promise.resolve(nextAction())
          .then(result => {  // Save results to the correct array index.
             resultArray[actionIndex] = result;
             return;
          }).then(doNextAction);
    }
  }

  // Now start up the original <limit> number of promises.
  // i advances in calls to doNextAction.
  let listOfPromises = [];
  while (i < limit && i < listOfCallableActions.length) {
    listOfPromises.push(doNextAction());
  }
  return Promise.all(listOfPromises).then(() => resultArray);
}

// Test harness:

function delay(name, ms) {
  return new Promise((resolve, reject) => setTimeout(function() {
    console.log(name);
    resolve(name);
  }, ms));
}

var ps = [];
for (let i = 0; i < 10; i++) {
  ps.push(() => delay("promise " + i, Math.random() * 3000));
}

throttleActions(ps, 3).then(result => console.log(result));

太感谢了!限制Promise的唯一解决方案。
2021-04-06 03:15:37

编辑

杰夫·鲍曼 (Jeff Bowman) 极大地改进了解决有意义的value观的答案。请随意查看此答案的历史记录,以了解为什么解析的值如此重要/有用。


节流阀

这个解决方案非常模仿原生 Promise.all

怎么都一样……

  • 尽快解决Promise
  • 以与输入相同的顺序解析一组值
  • 一遇到拒绝就拒绝

怎么不一样……

  • Number 参数限制了同时运行的 Promise 的数量
  • 数组输入接受Promise创建者(thunks);不是实际的Promise

// throttlep :: Number -> [(* -> Promise)]
const throttlep = n=> Ps=>
  new Promise ((pass, fail)=> {
    // r is the number of promises, xs is final resolved value
    let r = Ps.length, xs = []
    // decrement r, save the resolved value in position i, run the next promise
    let next = i=> x=> (r--, xs[i] = x, run(Ps[n], n++))
    // if r is 0, we can resolve the final value xs, otherwise chain next
    let run = (P,i)=> r === 0 ? pass(xs) : P().then(next(i), fail)
    // initialize by running the first n promises
    Ps.slice(0,n).forEach(run)
  })

// -----------------------------------------------------
// make sure it works

// delay :: (String, Number) -> (* -> Promise)
const delay = (id, ms)=>
  new Promise (pass=> {
    console.log (`running: ${id}`)
    setTimeout(pass, ms, id)
  })

// ps :: [(* -> Promise)]
let ps = new Array(10)
for (let i = 0; i < 10; i++) {
  ps[i] = () => delay(i, Math.random() * 3000)
}

// run a limit of 3 promises in parallel
// the first error will reject the entire pool
throttlep (3) (ps) .then (
  xs => console.log ('result:', xs),
  err=> console.log ('error:', err.message)
)

控制台输出

输入按顺序运行;解析结果与输入的顺序相同

running: 0
running: 1
running: 2
=> Promise {}
running: 3
running: 4
running: 5
running: 6
running: 7
running: 8
running: 9
result: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

实际使用

让我们看一个更实用的代码示例。此代码的任务是从服务器获取一组图像。这就是我们可以用来throttlep将同时请求的数量一次限制为 3 个的方式

// getImage :: String -> Promise<base64>
let getImage = url=> makeRequest(url).then(data => data.base64, reqErrorHandler)

// actions :: [(* -> Promise<base64>)]
let actions = [
  ()=> getImage('one.jpg'),
  ()=> getImage('two.jpg'),
  ()=> getImage('three.jpg'),
  ()=> getImage('four.jpg'),
  ()=> getImage('five.jpg')
]

// throttle the actions then do something...
throttlep (3) (actions) .then(results => {
  // results are guaranteed to be ordered the same as the input array
  console.log(results)
  // [<base64>, <base64>, <base64>, <base64>, <base64>]
})
你的测试风格提供了更清晰的输出,所以我也从你那里借用了 ^_^
2021-03-19 03:15:37
您的代码中存在错误(在完成之前会多次引发异常)。它可以通过像这样改变运行函数来解决: let run = (P,i)=> { if(r === 0) { pass(xs); } else if(P){ P().then(next(i), fail); } }; 感谢您和@JeffBowmansupportsMonica 为您提供的解决方案!
2021-03-23 03:15:37
@JeffBowman 很高兴与您合作。我也编辑了我的答案以防止人们感到困惑^_^
2021-04-05 03:15:37
感谢您的反馈!我在想像 那样的答案会很有用Promise.all,但在我的第一次通过时没有机会。在我要更新的答案中添加了 6 行,并感谢您。
2021-04-09 03:15:37

没有任何内置的东西,所以你必须建立自己的。AFAIK,也没有用于此的库。

首先,从“延迟”开始——一个允许外部代码解决它的Promise:

class Deferral<T> {
    constructor() {
        this.promise = new Promise<T>((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
        });
    }

    promise: Promise<T>;
    resolve: (thenableOrResult?: T | PromiseLike<T>) => void;
    reject: (error: any) => void;
}

然后就可以定义一个“等待队列”,它代表所有等待进入临界区的代码块:

class WaitQueue<T> {
    private deferrals: Deferral<T>[];

    constructor() {
        this.deferrals = [];
    }

    get isEmpty(): boolean {
        return this.deferrals.length === 0;
    }

    enqueue(): Promise<T> {
        const deferral = new Deferral<T>();
        this.deferrals.push(deferral);
        return deferral.promise;
    }

    dequeue(result?: T) {
        const deferral = this.deferrals.shift();
        deferral.resolve(result);
    }
}

最后,您可以定义一个异步信号量,如下所示:

export class AsyncSemaphore {
    private queue: WaitQueue<void>;
    private _count: number;

    constructor(count: number = 0) {
        this.queue = new WaitQueue<void>();
        this._count = count;
    }

    get count(): number { return this._count; }

    waitAsync(): Promise<void> {
        if (this._count !== 0) {
            --this._count;
            return Promise.resolve();
        }
        return this.queue.enqueue();
    }

    release(value: number = 1) {
        while (value !== 0 && !this.queue.isEmpty) {
            this.queue.dequeue();
            --value;
        }
        this._count += value;
    }
}

用法示例:

async function performActionsInParallel() {
    const semaphore = new AsyncSemaphore(10);
    const listOfActions = [...];
    const promises = listOfActions.map(async (action) => {
        await semaphore.waitAsync();
        try {
            await doSomething(action);
        }
        finally {
            semaphore.release();
        }
    });
    const results = await Promise.all(promises);
}

此方法首先创建一个节流阀,然后立即启动所有异步操作。每个异步操作都会首先(异步地)等待信号量空闲,然后执行操作,最后释放信号量(允许另一个信号量进入)。当所有异步操作完成后,检索所有结果。

警告:此代码 100% 完全未经测试。我什至没有尝试过一次。

您可以使用 pub-sub 模式来做到这一点。我对 typecipt 也不熟悉,不知道是浏览器还是后台出现这种情况。我将为此编写伪代码(假设它是后端):

//I'm assuming required packages are included e.g. events = require("events");
let limit = 10;
let emitter = new events.EventEmitter();

for(let i=0; i<limit; i++){
    fetchNext(listOfActions.pop());
}

function fetchNext(action){
    const actionResultPromise = doSomething(action);
    actionResultPromise.then((actionResult) => {
        console.log(`Action Done: ${actionResult}`);
        emitter.emit('grabTheNextOne', listOfActions.pop());
    });
}

emitter.on('grabTheNextOne', fetchNext);

EventEmitter 是 NodeJS 的一部分,如果你在 Node 中工作的话。如果在浏览器中,则可以使用普通事件模型。这里的关键思想是发布-订阅模式。

可以使用生成器限制 Promise。在下面的示例中,我们正在限制它们,以便

function asyncTask(duration = 1000) {
  return new Promise(resolve => {
    setTimeout(resolve, duration, duration)
  })
}


async function main() {
  const items = Array(10).fill(() => asyncTask()) {
    const generator = batchThrottle(3, ...items)
    console.log('batch', (await generator.next()).value)
    for await (let result of generator) {
      console.log('remaining batch', result)
    }
  }

  {
    const generator = streamThrottle(3, ...items)
    console.log('stream', await generator.next())
    for await (let result of generator) {
      console.log('remaining stream', result)
    }
  }

}

async function* batchThrottle(n = 5, ...items) {
  while (items.length) {
    const tasks = items.splice(0, n).map(fn => fn())
    yield Promise.all(tasks)
  }
}

async function* streamThrottle(n = 5, ...items) {
  while (items.length) {
    const tasks = items.splice(0, n).map(fn => fn())
    yield* await Promise.all(tasks)
  }
}
main().catch()