首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >async.each不会无错误地完成

async.each不会无错误地完成
EN

Stack Overflow用户
提问于 2018-05-26 02:10:09
回答 1查看 161关注 0票数 0

我有一个简单的函数,可以从url中得到单词计数。如果我有较低数量的urls,脚本就能工作。我一次只限制异步4。我看我的内存和cpu,它不会接近我的机器上的最大值。比方说,在大约70多个urls之后,没有错误。剧本就放在那里。我把它放在一个试接块里,但它永远抓不到。任何帮助都将不胜感激。

我尝试过提交forEach而不是异步,我也遇到了同样的问题。

代码语言:javascript
复制
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)
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-03 09:56:06

我认为问题在于这个after-load模块的同步实现,但是除非您得到一个实际的错误,否则很难判断(您可以在每一行上放置一些console.logs,查看代码实际卡在哪里--或者为了同样的目的使用调试器)。

不过,我建议的是使用适当的异步代码--我使用了一组1000个urls来运行下面的示例,并且它没有卡住--使用扰流引擎时,它的可读性也更强:

代码语言:javascript
复制
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)
;

实际上,我通过将第一行更改为:

代码语言:javascript
复制
StringStream.from(fs.createReadStream('./urls-list.txt'), 'utf-8')
    .lines()
    .setOptions({maxParallel: 4})
    // and so on.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50538717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档