限制Node.js的异步调用 [英] Limiting asynchronous calls in Node.js

查看:205
本文介绍了限制Node.js的异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node.js的应用程序,在本地获取文件列表并上传到服务器。这个列表可以包含成千上万的文件。

I've got a Node.js app that gets a list of file locally and uploads them to a server. This list could contain thousands of files.

for (var i = 0; i < files.length; i++) {
   upload_file(files[i]);
}

如果我与成千上万的文件执行此,upload_file将调用数千次全部一次,而最有可能的模具(或至少斗争)。在同步的世界里,我们会创建一个线程池,它限制在一定的数量的线程。有一个简单的办法来限制多少异步调用一次得到执行?

If I execute this with thousands of files, upload_file will get called thousands of times all at once, and most likely die (or at least struggle). In the synchronous world, we'd create a thread pool and limit it to a certain number of threads. Is there a simple way to limit how many asynchronous calls get executed at once?

推荐答案

像往常一样,我建议Caolan麦克马洪的异步模块

As usual, I recommend Caolan McMahon's async module.

请您 upload_file 函数需要一个回调,因为它的第二个参数:

Make your upload_file function take a callback as it's second parameter:

var async = require("async");

function upload_file(file, callback) {
    // Do funky stuff with file
    callback();
}

var queue = async.queue(upload_file, 10); // Run ten simultaneous uploads

queue.drain = function() {
    console.log("All files are uploaded");
};

// Queue your files for upload
queue.push(files);

queue.concurrency = 20; // Increase to twenty simultaneous uploads

这篇关于限制Node.js的异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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