Nadir
Nadir

Reputation: 1379

How to properly close phantomjs webpage after use?

I'm trying to read some data from 200+ web pages with PhantomJS and typescript/rxjs What I came up so far is this

Observable.fromPromise(phantom.createPage()).flatMap(jobPage => {
            return Observable.fromPromise(jobPage.open(url)).flatMap(status => {
                if (status !== "success") {
                    console.error("Couldn't load job page for url " + url + " Status: " + status);
                    jobPage.close();
                    return Observable.of(undefined)
                } else {
                    return Observable.fromPromise(jobPage.evaluate(function () {
                        //do some content reading, return data

                        return data;
                    }));
                }
            });
        })

And it works, but with every page it gets slower and slower, and finally ends with Memory Exhausted message from Phantom. I guess it's because I do not close the web pages I'm creating, but I dont have any idea how to do it such case (flatMap creates a new one, I need it for extraction later, and Observable.fromPromise() does not allow me to close the page after I'm done.

Any help is appreciated

Upvotes: 0

Views: 421

Answers (1)

Nadir
Nadir

Reputation: 1379

Ok, figured it out, just need to use

Observable.fromPromise(phantom.createPage()).flatMap(jobPage => {
            //stuff as before
        }).finally(function(){
                        jobPage.close();
                    })

Upvotes: 1

Related Questions