等待网络工作者拨打http电话完成 [英] waiting for web worker making http call to finish

查看:82
本文介绍了等待网络工作者拨打http电话完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建多个Web工作者进行http呼叫.我必须限制网络工作者的数量,因此我试图等待一些工作者完成.

I'm creating multiple web workers making http calls. I have to limit the number of web workers and so I'm trying to wait for some of the workers to finish.

这是我认为使用Promises可能会起作用的一个示例:

here's an example of what I thought might work using Promises:

anArray.map(async contact => {
    await new Promise((res, rej) => {
          const worker = new Worker('./http.worker', { type: 'module' });
          worker.onmessage = () => {
            res();
          };
          worker.postMessage(contact);
    });
});

我认为这将等待每个诺言解决,然后再继续进行下一个项目……但这不是.

I thought this would wait for each promise to resolve before moving on to the next item... but it's not.

我该怎么做才能使这项工作成功?或...我还考虑过要构建一组工人并运行一个递归循环,以检查/等待一个人可用...我愿意接受解决此问题的一般建议.

What could I do to make this work? or... I've also thought of building an array of workers and run a recursive loop that checks/waits for one to be available... I'm open to general suggestions for solving this.

推荐答案

.map()不承诺.它不会查看每次迭代的返回值来查看它是否是一个Promise,然后暂停循环.相反,它只是盲目地运行所有迭代.当您通过async回调从.map()返回promise时,这仅意味着您的.map()将产生一个promise数组,并且所有循环迭代将同时进行中",而不是排序.

.map() is not promise aware. It does not look at the return value from each iteration to see if it's a promise and then pause the loop. Instead, it just blindly runs all the iterations one after another. When you return promises from .map() which you are with the async callback, that just means that your .map() will produce an array of promises and all your loop iterations will be "in-flight" at the same time, not sequenced.

如果您要迭代循环并在每次迭代中暂停循环直到承诺得以解决,请使用常规的for循环:

If you want to iterate a loop and pause the loop in each iteration until a promise resolves, then use a regular for loop:

async function someFunc() {
    for (let contact of anArray) {
        await new Promise((res, rej) => {
              const worker = new Worker('./http.worker', { type: 'module' });
              worker.onmessage = () => {
                res();
              };
              worker.postMessage(contact);
        });
    }
}


仅供参考,JavaScript中的http调用是非阻塞且异步的,因此尚不清楚为什么要在WebWorkers中进行调用.除非您对结果进行大量的CPU处理,否则您可以在主线程中正常执行http请求,而不会阻塞它.


FYI, http calls in Javascript are non-blocking and asynchronous so it's not entirely clear why you're doing them in WebWorkers. Unless you have CPU-intensive processing of the result, you can do the http requests in the main thread just fine without blocking it.

FYI,对于处理数组的许多不同选项,在同一时间(您确定N的值是多少)的同时仅处理N个请求,请参见以下各种答案:

Also FYI, for a number of different options for processing an array, with only N requests in flight at the same time (where you decide what value N is), see these various answers:

runN(fn, limit, cnt, options):通过针对多个请求的API

pMap(array, fn, limit): rateLimitMap(array, requestsPerSec, maxInFlight, fn):每秒最大请求数的正确异步方法

mapConcurrent(array, maxConcurrent, fn): Promise.all()消耗了我所有的内存

Bluebird承诺库异步承诺库.

这篇关于等待网络工作者拨打http电话完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆