Reputation: 3454
I have a long-running script that loops across several different links to perform its work. I encountered a scenario where PhantomJS ran out of resources due to the page objects not being garbage collected since I'm reusing it. I have a simple example below. I close() the page object and create a new one at the end of each loop cycle, but after I do that Casper just hangs. What is the proper way to do this?
var links = ['http://www.google.com', 'http://www.yahoo.com'];
var casper = require('casper').create({
logging: 'error',
pageSettings: {
webSecurityEnabled: false
}
});
casper.start('http://www.amazon.com', function () {
this.echo(this.getTitle());
});
casper.eachThen(links, function(item) {
var url = item.data;
this.open(url).then(function (){
this.echo(this.getTitle());
}).then(function (){
casper.page.close();
casper.page = require('webpage').create();
});
});
casper.run(function (){
this.echo('Done');
});
This will output
Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Google
and then just hang. What am I missing?
Upvotes: 4
Views: 1783
Reputation: 11
var casper = require('casper').create();
var urls = ['http://google.com/', 'http://yahoo.com/'];
casper.start().eachThen(urls, function(response) {
this.thenOpen(response.data, function(response) {
this.echo('\n'+this.getTitle());
});
});
casper.run(function(){
this.die('\n'+'Done');
});
this worked for me. its from the documentation... I'm not sure casperjs requires you to close the page.
Upvotes: 1