Node.js 使用承诺构建请求队列?

Node.js 使用承诺构建请求队列?,node.js,asynchronous,async-await,promise,Node.js,Asynchronous,Async Await,Promise,我的目标是针对REST端点运行数据导入 我不想在启动新的请求之前等待请求得到解决。我想“模拟”并行连接 我不确定我是否有一个基本的知识问题 此代码创建子进程: const numchild = require('os').cpus().length; const SIZE = 1000; const SIZE_PER_CHILD = SIZE / numchild; for (let i = 0; i < numchild; i++) { const child = child_pr

我的目标是针对REST端点运行数据导入

我不想在启动新的请求之前等待请求得到解决。我想“模拟”并行连接

我不确定我是否有一个基本的知识问题

此代码创建子进程:

const numchild = require('os').cpus().length;
const SIZE = 1000;
const SIZE_PER_CHILD = SIZE / numchild;

for (let i = 0; i < numchild; i++) {
  const child = child_process.fork('./child.js');
  child.send({ start: i * SIZE_PER_CHILD, end: (i + 1) * SIZE_PER_CHILD });
  // Some more code...
}
const numchild=require('os').cpus().length;
常数大小=1000;
const SIZE_PER_CHILD=大小/numchild;
for(设i=0;i
然后,对于每个子进程,我希望生成一个用于导入的随机负载,并针对REST端点激发它:

process.on('message', async function({ start, end }) {
  const count = start;
  while (count < end) {
    const generatedData = 'I was generated! Yay!' + count;
    await axios.put('/api/v1/import', generatedData);
    count++;
  }
});
process.on('message',异步函数({start,end}){
常数计数=开始;
while(计数<结束){
const generatedData='我被生成了!耶!!'+计数;
等待axios.put('/api/v1/import',生成数据);
计数++;
}
});
上述方法将等待每个导入请求完成,然后触发下一个请求,直到所有子导入完成。不是我想要的

现在,我攻击的端点应该能够处理比我能够生成的请求更多的请求

我可以这样重写它:

process.on('message', async function({ start, end }) {
  const count = start;
  while (count < end) {
    const generatedData = 'I was generated! Yay!' + count;
    axios.put('/api/v1/import', generatedData).then(() => console.log('I am done with this one'));
    count++;
  }
});

process.on('message',异步函数({start,end}){
常数计数=开始;
while(计数<结束){
const generatedData='我被生成了!耶!!'+计数;
put('/api/v1/import',generatedData)。然后(()=>console.log('I done with this one');
计数++;
}
});
当然,这种方法的问题是,所有请求都会在几秒钟内生成,并针对端点触发。我想这更像DOS风格。也不是我想要的

我希望实现的是:每个子进程有15个开放连接。如果一个请求完成,请排队等待下一个请求,直到有15个请求再次挂起

所以我试了一下:

process.on('message', async function({ start, end }) {
  const count = start;
  let queue = [];
  while (count < end) {
    if (queue.length === 15) {
      queue = queue.filter(async (promise) => {
        const state = await promiseState(promise);
        return state !== 'fulfilled';
      });
    } else {
      const generatedData = 'I was generated! Yay!' + count;
      queue.push(axios.put('/api/v1/import', generatedData).then(() => console.log('I am done with this one')));
      count++;
    }
  }
});

function promiseState(p) {
  const t = {};
  return Promise.race([p, t])
    .then(v => (v === t) ? "pending" : "fulfilled", () => "rejected");
}
process.on('message',异步函数({start,end}){
常数计数=开始;
让队列=[];
while(计数<结束){
if(queue.length==15){
队列=队列.过滤器(异步(承诺)=>{
const state=等待允诺(承诺);
返回状态!=“已完成”;
});
}否则{
const generatedData='我被生成了!耶!!'+计数;
push(axios.put('/api/v1/import',generatedData.),然后(()=>console.log('I done with this'));
计数++;
}
}
});
功能承诺状态(p){
常数t={};
回报承诺。种族([p,t])
。然后(v=>(v===t)?“待定”:“已完成”,“已拒绝”);
}
也不起作用,也没有意义,对吧?filter函数返回承诺,因此我尝试执行的操作不起作用


有什么办法可以做到这一点吗?

试试
p-queue
。下面,并发设置为3,这意味着在此队列中一次最多执行3个调用:

import PQueue from 'p-queue';

const queue = new PQueue({
  concurrency: 3,
});

process.on('message', async function ({ start, end }) {
  var calls = [];
  var count = start;
  while (count < end) {
    const generatedData = 'I was generated! Yay!' + count;

    calls.push(
      queue.add(() => {
        return axios
          .put('/api/v1/import', generatedData)
          .then(() => console.log('I am done with this one'));
      })
    );
    count++;
  }
  var results = await Promise.all(calls);
});
从“p-queue”导入PQUE;
const queue=新的PQUE({
并发性:3,
});
process.on('message',异步函数({start,end}){
var调用=[];
var计数=开始;
while(计数<结束){
const generatedData='我被生成了!耶!!'+计数;
打电话,推(
queue.add(()=>{
返回轴
.put('/api/v1/import',generatedData)
.then(()=>console.log('I done with this');
})
);
计数++;
}
var结果=等待承诺。全部(调用);
});

您可以看看哪些应该做您想做的一切。要么直接使用它,要么检查他们使用的代码。老实说,我不知道该如何使用它。遇到同样的问题。。。