我有一个简单的函数,可以从url中得到单词计数。如果我有较低数量的urls,脚本就能工作。我一次只限制异步4。我看我的内存和cpu,它不会接近我的机器上的最大值。比方说,在大约70多个urls之后,没有错误。剧本就放在那里。我把它放在一个试接块里,但它永远抓不到。任何帮助都将不胜感激。
我尝试过提交forEach而不是异步,我也遇到了同样的问题。
const async = require('async')
const wordcount = require('wordcount')
const afterLoad = require('after-load')
const htmlToText = require('html-to-text')
function getWordCount(urls, cb) {
async.eachLimit(urls, 4, function(url, cbe) {
try {
let html = afterLoad(url) // https://www.npmjs.com/package/after-load
let text = htmlToText.fromString(html)
let urlWordCount = wordcount(text) // https://www.npmjs.com/package/wordcount
console.log(url, urlWordCount)
cbe(null)
} catch(err) {
console.log(err)
urlWordCount = 0
console.log(url, urlWordCount, err)
cbe(null)
}
}, function(err) {
console.log("finished getting wordcount", err)
if (err) {
cb(err)
} else {
cb(null)
}
})
}
getWordCount(["https://stackoverflow.com/", "https://caolan.github.io/async/docs.html#eachLimit"], function(err){
console.log(err)
})发布于 2018-06-03 09:56:06
我认为问题在于这个after-load模块的同步实现,但是除非您得到一个实际的错误,否则很难判断(您可以在每一行上放置一些console.logs,查看代码实际卡在哪里--或者为了同样的目的使用调试器)。
不过,我建议的是使用适当的异步代码--我使用了一组1000个urls来运行下面的示例,并且它没有卡住--使用扰流引擎时,它的可读性也更强:
const {StringStream} = require('scramjet');
const wordcount = require('wordcount');
const fetch = require('node-fetch');
const htmlToText = require('html-to-text');
const {promisify} = require('util');
StringStream.fromArray(["https://stackoverflow.com/", "https://caolan.github.io/async/docs.html#eachLimit"])
.setOptions({maxParallel: 4})
.parse(async url => ({
url,
response: await fetch(url)
}))
.map(async ({url, response}) => {
const html = await response.text();
const text = htmlToText.fromString();
const count = wordcount(text);
return {
url,
count
};
})
.each(console.log)
;实际上,我通过将第一行更改为:
StringStream.from(fs.createReadStream('./urls-list.txt'), 'utf-8')
.lines()
.setOptions({maxParallel: 4})
// and so on.https://stackoverflow.com/questions/50538717
复制相似问题