簡體   English   中英

沒有內存過載的Node.js API請求循環

[英]Node.js API request loop without a memory overload

這可能看起來很奇怪,但是我試圖盡可能多地刷新API,直到response.data值達到一定數量為止。 這是我的下面的代碼

const axios = require('axios');
const api = require('./api.json');
const infinity = 0;

let url = 'http://api.site.com/api.php?i=${api[item].appid}';

function api_call(item){
  axios.get(url)
    .then(response => {
      let id = response.data.id;
      if (id > 10){
        return true;
      }else{
        return false;
      }
    })
    .catch(error => {
      console.log(error);
    });
  }

  function loop(variable) {
    while (infinity < 1) {
      if (api_call(variable) > true) {
        break;
        console.log('Found');
      }
      console.log('-');
    }
  }

這可能看起來很奇怪,但是顯然,我已經對其進行了修改,盡管該代碼仍然可以實現與我嘗試使用代碼實現的目標類似的目標。 基本上,它監視API,直到值更改觸發操作為止。 可能是我做錯了,但是當我運行這段代碼時(只有更改是API和值名稱),我開始使用大量內存,直到程序停止並給我這個錯誤

<--- Last few GCs --->

[11524:000001E5724778C0]    93390 ms: Mark-sweep 1395.2 (1426.2) -> 1394.5 (1425.7) MB, 629.4 / 0.0 ms  (average mu = 0.139, current mu = 0.053) allocation failure scavenge might not succeed
[11524:000001E5724778C0]    94039 ms: Mark-sweep 1395.4 (1425.7) -> 1394.8 (1426.2) MB, 638.3 / 0.0 ms  (average mu = 0.079, current mu = 0.016) allocation failure scavenge might not succeed


<--- JS stacktrace --->

連同更多信息(如果需要,我可以編輯該帖子並將其包括在內),我相信這是由於我如何收集數據而引起的,可能是有些程序忘記了,我不確定並不能做到。找不到與此相關的任何類似主題。 在此先感謝您的幫助。 如果需要更多信息,請問我會添加更多。 在此處輸入圖片說明

我玩了一會兒,這就是我最終開始工作的原因。

     const axios = require('axios');
     const api = require('./api.json');
     const infinity = 0;   

    function api_call(item) {
      let url = `http://api.site.com/api.php?i=${api[item].appid}`;
        axios.get(url)
    .then(response => {
      let id = response.data.id;
      if (id > 10){
        return true;
      }else{
        return false;
      }
    })
    .catch(error => {
      console.log(error);
    });
  }

    async function checkAPI(variable) {
      const res = await api_call()

      if (res) {
        //success
      } else {
        //Fail
      }
    }

    checkAPI();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM