2013-05-22 76 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/