简体   繁体   English

Axios 在之前完成后循环请求

[英]Axios requests in a loop after previous completes

The application I am building involves sending video frames to the server for processing.我正在构建的应用程序涉及将视频帧发送到服务器进行处理。 I am using Axios to send a POST request to the server.我正在使用 Axios 向服务器发送 POST 请求。 I intend to send about 3-5 frames(POST request) per second.我打算每秒发送大约 3-5 帧(POST 请求)。 I need to send one after the previous request completes because the next request has data in the request body which depends on the response of the previous request.我需要在前一个请求完成后发送一个,因为下一个请求在请求正文中有数据,这取决于前一个请求的响应。

I tried running it in a loop but that won't do because the next one starts before the previous one completed.我尝试在循环中运行它,但不会这样做,因为下一个在前一个完成之前开始。 In the current code, I have something like this -在当前代码中,我有这样的东西 -

const fun = () => {
    axios.post(url,data)
         .then((res) => {
              // process the response and modify data accordingly
              // for the next request
          })
         .finally(() => fun());
};

This way I am able to achieve the requests to be one after another continuously.这样我就能够不断地实现一个又一个的请求。 But I am unable to control the number of requests that are being sent per second.但我无法控制每秒发送的请求数。 Is there any way I can limit the number of requests per second to be say at max 5?有什么办法可以限制每秒的请求数最多为 5 个?

Additional Info : I am using this for a web app where I need to send webcam video frames(as data URI) to the server for image processing and the result back to the client after it.附加信息:我将其用于 web 应用程序,我需要将网络摄像头视频帧(作为数据 URI)发送到服务器进行图像处理,然后将结果返回给客户端。 I am looking to limit the number of requests to reduce the internet data consumed by my application and hence would like to send at the max 5 requests per second(preferably evenly distributed).我希望限制请求的数量以减少我的应用程序消耗的互联网数据,因此希望每秒最多发送 5 个请求(最好是均匀分布的)。 If there's a better way that Axios POST requests for this purpose, please do suggest:)如果有更好的方法让 Axios POST 请求为此目的,请建议:)

An amazing library called Bottleneck can come to your aid here.一个名为瓶颈的惊人图书馆可以在这里为您提供帮助。 You can limit the number of request per second to any number you want using the following code:您可以使用以下代码将每秒请求数限制为您想要的任何数量:

const Bottleneck = require("bottleneck");
const axios = require('axios');
const limiter = new Bottleneck({
    maxConcurrent: 1,
    minTime: 200
});

for(let index = 0; index < 20; index++){
    limiter.schedule(() => postFrame({some: 'data'}))
        .then(result => {
            console.log('Request response:', result)
        })
        .catch(err => {
            console.log(err)
        })
}


const postFrame = data => {
    return new Promise((resolve, reject) => {
        axios.post('https://httpstat.us/200', data)
            .then(r => resolve(r.data))
            .catch(e => reject(e))
    });
}

By manipulating the limiter通过操纵限制器

const limiter = new Bottleneck({
    maxConcurrent: 1,
    minTime: 200
});

You can get your axios request to fire at any rate with any concurrency.您可以让您的 axios 请求以任何速率以任何并发方式触发。 For instance to limit the number of request per second to 3, you would set minTime to 333 .例如,要将每秒请求数限制为 3,您可以将minTime设置为333 Stands to reason, if you want to limit it to 5 per second, it would have to be set to 200 .按理说,如果要将其限制为每秒 5 次,则必须将其设置为200

Please refer the the Bottleneck documentation here: https://www.npmjs.com/package/bottleneck请参阅此处的瓶颈文档: https://www.npmjs.com/package/bottleneck

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM