2013-05-22 75 views
7

我運行在遠程的webdriver模式phantomjs與phantomjs --webdriver 8910,然後讓使用硒python綁定多頁的東西,如:使用selenium webdriver限制phantomjs的內存使用情況?

wd = webdriver.PhantomJS(port=8910) 
for url in big_url_list: 
    wd.get(url) 
    # do something here, e.g. wd.save_screenshot or print wd.page_source 
    wd.quit() 

wd.quit()顯得無能爲力。用top監視進程表明,隨着更多頁面的檢索,phantomjs進程的內存使用量不斷增加。如果我重新啓動phantomjs進程,那麼內存使用量會降低,並開始穩定爬升。

close method mentioned in the phantomjs docs聽起來很有希望,但我看不到通過WebDriver protocol調用該方法的任何方法。

有沒有(i)一種方法可以在進行多次請求時保持幻像的內存使用量,或者(ii)監控和定期重啓phantomjs進程的可靠方法,使內存永不失控?

回答

1

只有在關閉WebDriver會話時,Ghostdriver纔會調用PhantomJS網頁關閉方法。 當達到內存限制時,您可以嘗試使用runit重新啓動PhantomJS。創建bash腳本如下:

#!/bin/sh 
exec 2>&1 
exec chpst -u your_user -m 104857600 /usr/bin/phantomjs --webdriver=8910 

-m標誌設置內存限制爲100MB。

上面的腳本放置一些空目錄下,名稱爲 「跑」:

| 
`-phantomjs-runit-+ 
        `-run 

現在你可以開始phantomjs

sv start ./phantomjs-runit/