是否可以在node.js中为多个独立任务创建多个计时器?

时间:2013-03-01 20:13:14

标签: javascript node.js timer-jobs

我有一个node.js服务器接受来自网页的请求。收到“START”请求后,我希望能够每隔n秒生成一个在后台执行任务的计时器。这可行吗?什么是一个很好的例子?一个简单的psudo代码如下:

app.get("myindependenttasks/starttask/:userid"){
   //start a simple timer to handle to userid
   //continue to run the timers endlessly while 
   //while this call returns a "Task running" status
   //to the user.
   //Other users should be able to run their own tasks.
}

如果用户请求的数量大约是1000个正在运行的任务,那么是否有任何缺点。

1 个答案:

答案 0 :(得分:1)

你可以使用setIntervalhttp://nodejs.org/api/timers.html),但我会犹豫不决这样做1000个。

相反,您应该运行一个setInterval,并在此任务中为所有已连接的用户运行任务。

因此,当用户点击“myindependenttasks / starttask /:userid”时,会将其添加到要处理的用户列表中,并在您的定期任务中逐个浏览该列表。

相关问题