简体   繁体   English

很多HTTP请求并另存为json:连接ETIMEDOUT

[英]A lot of HTTP requests and save as json: connect ETIMEDOUT

I have a problem with my code. 我的代码有问题。 I need to capture a lot of data by API. 我需要通过API捕获大量数据。 First, i had a problem with memory and now I see: connect ETIMEDOUT error 首先,我的内存有问题,现在我看到:连接ETIMEDOUT错误

My code: 我的代码:

 var fetch = require('node-fetch'); const async = require('async'); const request = require('request'); var jsonfile = require('jsonfile'); var file = '/temp/data.json' let urls = []; for (var i = 1; i <= 608469; i++) { let url = "https://api.demo.com/v2.1/people?user_key=auth_key&page="+i+"&sort_order=created_at%20DESC"; urls.push(url); } function httpGet(url, callback) { const options = { url : url, json : true }; request(options, function(err, res, body) { callback(err, body); } ); } async.map(urls, httpGet, function (err, res){ if (err) return console.log(err); console.log(typeof(res)); jsonfile.writeFileSync(file, res); }); 

Just replace async's map method with eachLimit . 只需用eachLimit替换异步的map方法。 Key here is limit . 这里的关键是limit It still will run http request for each link, but in no more than limit paraller threads. 它仍然会为每个链接运行http请求,但最多只能limit并行线程。 Here is a doc: https://caolan.github.io/async/docs.html#eachLimit 这是一个文档: https : //caolan.github.io/async/docs.html#eachLimit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM