我试图从我已经存储在一个数组中的一堆HTML页面拉一个字符串。我有以下代码:处理内存时使用节点和jsdom蜘蛛网站
const jsdom = require('jsdom')
desc('Import pages');
task('handleSpots', [], function (params) {
allSpots.forEach(function(spotUrl){
handleSpot(spotUrl)
})
});
function handleSpot (href) {
jsdom.env(
href,
["http://code.jquery.com/jquery.js"],
function (err, window) {
if (err) {
console.log(host+href+" "+err)
return
}
const data = {url: host+href}
data['name'] = window.$("h1.wanna-item-title-title a").text()
console.log(data['name'])
window.close()
}
);
}
allSpots数组中有大约600个URL。当我运行此,我收到了一堆错误:
/the_hook/index.html Error: read ECONNRESET
这正好一堆网址,显示一些名字,最后我得到这个错误。
<--- Last few GCs --->
80660 ms: Scavenge 1355.3 (1460.0) -> 1355.3 (1460.0) MB, 2.3/0 ms (+ 1.4 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
82149 ms: Mark-sweep 1355.3 (1460.0) -> 1354.8 (1460.0) MB, 1488.7/0 ms (+ 2.8 ms in 2 steps since start of marking, biggest step 1.4 ms) [last resort gc].
83657 ms: Mark-sweep 1354.8 (1460.0) -> 1354.6 (1460.0) MB, 1508.2/0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x38f1b4237339 <JS Object>
1: create [native v8natives.js:~755] [pc=0x22e6902f1923] (this=0x38f1b4236b61 <JS Function Object (SharedFunctionInfo 0x38f1b4236ad1)>,an=0x1590d58f6941 <an Object with map 0x1b19e3c1e251>,aD=0x38f1b4204131 <undefined>)
2: arguments adaptor frame: 1->2
3: createImpl [/Users/craig/Programming/node_wannasurf_importer/node_modules/jsdom/lib/jsdom/living/generated/Text.js:~90] [pc=0x22e...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Abort trap: 6
只有当allSpots数组中有超过125个项目时才会发生这种情况。少于这一点,一切正常。
我对节点很新,但我假设Javascript试图在同一时间获取太多这些页面,并最终耗尽内存。理想情况下,我可以写的东西处理100,等到这样做了,然后移动到下一个100
我尝试这样做: async.eachLimit(allSpots,100,handleSpot) 但是,只处理第100然后停止。
我也试过: async.eachSeries(allSpots,handleSpot) 但是这只处理第一个url并停止。
我处于死路一条,所以我非常感谢任何人都可以给我的建议。 谢谢,
克雷格