简体   繁体   English

使用selenium webdriver限制phantomjs的内存使用?

[英]Limit memory usage of phantomjs using selenium webdriver?

I'm running phantomjs in Remote WebDriver mode with phantomjs --webdriver 8910 and then getting many pages using the Selenium python bindings with something like: 我正在使用phantomjs --webdriver 8910在Remote WebDriver模式下运行phantomjs,然后使用Selenium python绑定获取许多页面,例如:

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()

The wd.quit() appears to do nothing. wd.quit()似乎什么都不做。 Monitoring the process with top shows that the memory usage of the phantomjs process continues to increase as more pages are retrieved. 使用top监视进程表明,当检索到更多页面时,phantomjs进程的内存使用量会继续增加。 If I restart the phantomjs process, then the memory usage drops low and begins it's steady climb again. 如果我重新启动phantomjs进程,那么内存使用率会下降并开始再次稳定攀升。

The close method mentioned in the phantomjs docs sounds promising, but I don't see any way to call that via the WebDriver protocol . phantomjs文档中提到close方法听起来很有希望,但我没有看到通过WebDriver协议调用它的方法。

Is there (i) a way to keep the memory usage of phantomjs down when making many requests, or (ii) a reliable way to monitor and periodically restart the phantomjs process so that the memory never gets out of hand? 有没有(i)在发出许多请求时保持phantomjs的内存使用率下降的方法,或者(ii)监视和定期重启phantomjs进程的可靠方法,以便内存永远不会失控?

This SO question suggests the use of release which has been deprecated in favor of close . 这个SO问题建议使用已被弃用的发布 ,以支持close It seems that the python webdriver API exposes the close method ? 似乎python webdriver API 暴露了close方法

PhantomJS webpage close method calls by Ghostdriver only when you close WebDriver session. 只有在关闭WebDriver会话时,才能通过Ghostdriver调用PhantomJS网页关闭方法。 You can try to use runit to restart PhantomJS when memory limit was reached. 当达到内存限制时,您可以尝试使用runit重新启动PhantomJS。 Create bash script as follows: 创建bash脚本如下:

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

-m flag set memory limit to 100MB. -m标志将内存限制设置为100MB。

Place above script under some empty directory with name "run": 将上面的脚本放在一个名为“run”的空目录下:

 |
 `-phantomjs-runit-+
                   `-run

Now you can start phantomjs 现在你可以开始幻影了

sv start ./phantomjs-runit/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM