我想做一个模拟,比如在node.js中登录到web应用程序多个用户。
我有一个函数login(),我在for循环中执行它,以模拟许多用户的登录。
但是,当maxThreads >5时,有时不会收到回调在函数login()中请求的响应,我的代码是:
var request = require("request");
var cheerio = require("cheerio");
function login(i){
request({
uri: "http://example.com/sign_in",
method: "POST",
timeout: 1000,
followAllRedirects: true,
form: {
email: "mail@example.com",
password: "password"
}
}, function (error, response, body) {
var $ = cheerio.load(body);
var title = $("title");
console.log(title.html()+ "-" + i );
});
}
var maxThreads = 10
for(i=0; i<maxThreads; i++){
login(i)
}示例输出:
null-5
null-6
null-7
null-8
null-9
Exapmle title-0
Exapmle title-1
Exapmle title-3
Exapmle title-2
Exapmle title-4我的问题: whan我有更多的线程-我有更多的空响应
发布于 2014-03-10 14:07:04
您可以使用异步模块而不是for循环。这样,您就可以更好地控制控制流。
https://stackoverflow.com/questions/22300019
复制相似问题