setTimeout是否有限制? [英] Is there any limit to setTimeout?

查看:95
本文介绍了setTimeout是否有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专门谈论(服务器端)V8,并假设我不担心准确性,因为我可以检测并补偿它,我是否可以从字面上理解设置成千上万个相对简单的超时间隔(间隔几秒钟)从彼此使用setTimeout,而不面临除RAM以外的任何其他限制?如果我要使用一个在任何给定时间可能有数千个计划的超时的系统,我应该注意什么吗?

Specifically talking about (server side) V8, and assuming I'm not concerned about accuracy because I can detect and compensate for it, could I literally set up thousands of relatively simple timeouts several seconds apart from each other using setTimeout without facing any other limit other than RAM? Is there any catch I should be aware of if I were to use a system where there may be thousands of scheduled timeouts at any given time?

出于记录目的,我已阅读 JavaScript计时器的工作方式,所以无需指出那里已经涵盖的所有内容:)我知道node.js是单线程的,如果时间太长,计时器可能会阻塞其他计时器,等等.

For the record I've read John's Resig excellent article on How Javascript Timers work so no need to point out anything that's already covered there :) I'm aware node.js is single threaded, timers can block other timers if they take too long, etc.

PS:我严格地试图理解我所描述的内容的可行性,无需指出肯定有更好的方法来完成您打算做的事情!".

PS: I'm strictly trying to understand how viable is what I describe, no need to point out "there's surely a better way to do what you intend to do!".

推荐答案

您可能遇到的唯一实际限制是节点可用的内存量.使用以下代码进行测试.我使用oneMillion和int32Max成功运行了以下示例.使用int64Max时,我从节点收到以下错误.我正在使用具有4GB RAM的64位Windows.

The only real world limit you may come up against is the amount of memory available to node. Use the following code to test. I successfully ran the example below using oneMillion and int32Max. When using int64Max, I received the following error from node. I'm using 64bit windows with 4gb of RAM.

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

要测试的节点代码:

var util = require('util');
var int64Max = 9007199254740992; 
var int32Max = 2147483647;
var oneMillion = 1000000;
var tenThousand = 10000;
var counter = 0;

//Exchange the limiter with one of the above vars to test.
for (var i = 0; i < oneMillion; i++){   
     setTimeout(log, 1);
     //Required as the timeout/callback method will not be called until the loop ends due 
     //to node/js being single threaded.
     util.log('loop:' + i);
}

function log(){
     util.log('callback: ' + counter++);
}

这篇关于setTimeout是否有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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