首页 > phantomjs导致cpu使用率过高

phantomjs导致cpu使用率过高

phantomjs导致cpu使用率过高

场景

问题

描述

有个需求是这样的,下载一连串的html页面,为了支持phantomjs,我采用了最新的casperjs(不稳定版)
php:
foreach() {

$result = downloadHTML();
if $result == false {
       retry;(重试3次)
}

}

casper.js
var casper = require('casper').create();
casper.start(url);
fs.write(htmlFile, this.getHTML(), 'w');
if success {

exit(0);

} else {

exit(1);

}

类似这样的操作,结果发现有时候phantomjs卡住了,然后cpu使用率达到了100%,想问问我代码的思路有问题吗?如何有效防止?


请教下phantomjs抓取了一些网址之后导致CPU负载过高,导致云主机挂机,怎么有效解决?


找到原因了,因为在监听新窗口时候按照了它官方的写法

casper.waitForPopup(/popup\.html$/, function() {
    this.test.assertEquals(this.popups.length, 1);
});

casper.withPopup(/popup\.html$/, function() {
    this.test.assertTitle('Popup title');
});

然后改成

casper.waitForPopup(/popup\.html$/, function() {
    this.test.assertEquals(this.popups.length, 1);
    casper.withPopup(/popup\.html$/, function() {
        this.test.assertTitle('Popup title');
    });
});

妈蛋,按照官方的写法,脚本在一些情况下是有问题的,但是脚本出错了以后phantomjs没有退出,反而导致了cpu使用率高达了将近100%。

【热门文章】
【热门文章】