【问题标题】:Underscore.js throttle not workingUnderscore.js 油门不起作用
【发布时间】:2016-12-11 16:58:06
【问题描述】:

目标

找出我的代码出了什么问题,或者 underscore.js 节流阀是否正常工作。

背景

我在一个文件中有大量邮政编码,我正在阅读这些编码并将它们粘贴到控制台上。

我正在尝试使用Underscore.js throttle() function,但是我的代码在两次运行后停止(即使我有几十个),并且其余的值从未打印出来。

代码

我的代码在一个非常简单的 NodeJS 项目中。我创建了一个MCVE 我所面临的情况:

"use strict";

//requiremetns
let fs = require('fs');
let readLine = require('readline');
let _ = require('underscore');

//constants
const INPUT_FILE = 'dataset.txt';
const RADIX_CONVERSATION = 10;
const THROTTLE_DELAY = 500;

let init = function() {

  let lineReader = readLine.createInterface({
    input: fs.createReadStream(INPUT_FILE),
    output: process.stdout,
    terminal: false
  });

  let throttledRequestFn = _.throttle(requestFn, THROTTLE_DELAY);

  lineReader.on('line', function(line) {
    line = line.trim();

    if (_.isNaN(parseInt(line, RADIX_CONVERSATION))) {
      //Do some stuff
    }
    else {
      throttledRequestFn('mahCountry', line);
    }
  });
};

let requestFn = function(country, postalCode){
  console.log('request for ' + country + ' and postal ' + postalCode + ' done');
  return false;
};


init();

在这里,我首先从读取文件开始,一次一行。然后如果我正在阅读的行是一个数字,我打印一些东西,否则什么都没有。

以下是测试文件:

Vietnam
000000  
100000
160000  
170000  
180000  
200000
220000
230000
240000  
250000  
260000
270000
280000
290000  
300000  
310000  
320000  
330000
350000
360000
380000
390000
400000  
410000  
420000
430000  
440000
460000  
480000
510000
520000
530000
550000
560000
570000  
580000  
590000
600000
620000
630000
640000  
650000  
660000
670000
700000
790000  
800000
810000  
820000
830000
840000  
850000
860000  
870000  
880000  
890000  
900000
910000
920000
930000  
940000
950000
960000
970000

问题

在我看来,我的代码应该每秒发出 2 个请求,每个请求之间有 500 毫秒的延迟。它应该打印测试文件中的所有代码。 但是,我从未见过任何超过第二个值的东西!为什么会这样?

【问题讨论】:

    标签: javascript node.js underscore.js throttling


    【解决方案1】:

    throttle 函数按预期工作。来自documentation

    对于发生速度超过您跟上的速度限制事件很有用。

    这意味着你的包装函数可能会被调用比你想要的更少

    您真正想要的可能是某种队列。下划线不提供,但异步库提供:http://caolan.github.io/async/docs.html#.queue

    let fs = require('fs');
    let readLine = require('readline');
    let _ = require('async');
    // import the async library
    let async = require('async');
    
    const INPUT_FILE = 'dataset.txt';
    const RADIX_CONVERSATION = 10;
    
    // create the queue
    var q = async.queue(function(task, callback) {
      // make sure the queue task calls the callback only after the THROTTLE_DELAY
      setTimeout(function () {
        requestFn(task.country, task.postalCode);
        callback();
      }, THROTTLE_DELAY);
    
    }, 1)
    
    q.drain = function () {
      console.log('all items have been processed');
    };
    
    let init = function () {
      let lineReader = readLine.createInterface({
        input: fs.createReadStream(INPUT_FILE),
        output: process.stdout,
        terminal: false
      });
    
      lineReader.on('line', function(line) {
        line = line.trim();
    
        if (_.isNaN(parseInt(line, RADIX_CONVERSATION))) {
          // Do some stuff
        }
        else {
          // Add the line to the Queue, to be executed later
          q.push({country: 'mahCountry', postalCode: line});
        }
      });
    };
    
    let requestFn = function(country, postalCode){
      console.log('request for ' + country + ' and postal ' + postalCode + ' done');
      return false;
    };
    
    init();
    

    注意在处理队列中元素的函数中使用了setTimeout。这样一来,您仍然每 500 毫秒只发出一个请求,但保证会发出所有请求。

    【讨论】:

    • 它是如何按预期工作的?我有 40 个邮政编码,但我只看到它被调用了 2 次:S 我错过了什么?它不应该每 500 毫秒调用一次函数吗?这是我从文档中得到的。
    • 它按照 Underscore 开发者的意图工作。 throttle 不会将您的函数调用排队以稍后执行它们,它只是忽略它们直到您的油门延迟结束,然后执行仅一个,此时它开始再次忽略调用。正如我所说:您可能想要使用队列。
    • 那么,我可以使用队列和 underscore.js 节流吗?
    • 您根本不必使用油门。我举个例子,等一下。
    • 等等!我非常感谢您的积极主动,但是由于我正在查询外部 API 并且 QPS 有限,我需要限制我的请求(在这种情况下,我需要限制我对 requestFn 的调用)!这就是为什么 underscore.js throttle 一开始看起来如此有前途的原因 =( 另外,对这种态度表示敬意!
    猜你喜欢
    • 2012-12-22
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多