简体   繁体   English

无法通过 Python 中的 Selenium 杀死 Chrome 进程并使用 ChromeDriver 和 Chrome 耗尽内存

[英]Unable to kill Chrome process and running out of memory with ChromeDriver and Chrome through Selenium in Python

I have a crawling process that kicks off selenium in a custom class that looks like this:我有一个爬行过程,它在一个自定义类中启动 selenium,如下所示:

class BrowserInterface:

def __init__(self, base_url, proxy_settings):

    self.base_url = base_url

    self.display = Display(visible=0, size=(1024, 768))
    self.display.start()

    proxy_argument = '--proxy-server={0}'.format(PROXY_URL.format(
        proxy_settings.get('proxy_host'),
        proxy_settings.get('proxy_port')
    ))

    logger.debug(proxy_argument)

    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument(proxy_argument)

    selenium_chrome_driver_path = os.path.join(settings.DEFAULT_DRIVER_PATH,
                                               settings.CHROME_DRIVERS[settings.CURRENT_OS])

    self.driver = webdriver.Chrome(executable_path=selenium_chrome_driver_path, chrome_options=options)

def visit(self, url):
    url = urljoin(self.base_url, url)
    self.driver.get(url)

def body(self):
    soup = BeautifulSoup(self.driver.page_source)
    return soup.find("body").text

def quit(self):
    self.driver.quit()
    self.display.stop()

This BrowserInterface class is initialized in a batch queue and the quit() method is called at the end of the batch.此 BrowserInterface 类在批处理队列中初始化,并在批处理结束时调用 quit() 方法。 There are no issues starting chrome and getting the data.启动 chrome 和获取数据没有问题。 The trouble is, at the end of each job when the quit() method is called chrome goes into zombie mode.问题是,在每个作业结束时,当调用 quit() 方法时,chrome 会进入僵尸模式。 When the next BrowserInterface is initialized it starts a new chrome instance.当下一个 BrowserInterface 被初始化时,它会启动一个新的 chrome 实例。 Due to this, the box is running out of memory.因此,盒子内存不足。 I've tried running the a kill command as well on the chrome process but it stays running.我也尝试在 chrome 进程上运行 a kill 命令,但它仍在运行。 Any direction would be greatly appreciated as I'm about to pull my hair out over this.任何方向都将不胜感激,因为我即将把头发拉出来。

Running on Ubuntu 18.04, Google Chrome 70.0.3538.110, ChromeDriver 2.44, Python3.6.6在 Ubuntu 18.04、谷歌浏览器 70.0.3538.110、ChromeDriver 2.44、Python3.6.6 上运行

Thanks in advance!提前致谢!

From your code trials it is pretty much evident you have invoked self.driver.quit() which should have worked perfect.从您的代码试验中,很明显您已经调用self.driver.quit() ,它应该可以完美运行。

However as the box is still running out of memory due to zombie chrome processes you took the right approach to execute the a kill command and you can add the following solution within the quit() method:但是,由于僵尸chrome 进程导致该框仍然内存不足,您采取了正确的方法来执行 a kill命令,您可以在quit()方法中添加以下解决方案:

from selenium import webdriver
import psutil

driver = webdriver.Chrome()
driver.get('http://google.com/')

PROCNAME = "chrome" # to clean up zombie Chrome browser
#PROCNAME = "chromedriver" # to clean up zombie ChromeDriver
for proc in psutil.process_iter():
    # check whether the process name matches
    if proc.name() == PROCNAME:
        proc.kill()

Please see: https://stackoverflow.com/a/49756925请参阅: https ://stackoverflow.com/a/49756925

Creating bash as the root process allows for better 'zombie handling'.创建 bash 作为根进程允许更好的“僵尸处理”。 Python isn't meant to be run as a top level process which causes the zombies. Python 并不意味着作为导致僵尸的顶级进程运行。

暂无
暂无

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

相关问题 无法通过 Selenium 和 Python 使用 ChromeDriver 和 Chrome 登录 ebay 帐户 - Unable to signin into ebay account using ChromeDriver and Chrome through Selenium and Python chromedriver 不再运行,因此 ChromeDriver 假设 Chrome 使用 Selenium 到 Python 发生崩溃错误 - chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed error using Selenium through Python SessionNotCreatedException:此版本的 ChromeDriver 仅支持 Chrome 版本 84 使用 ChromeDriver 和 Chrome 通过 Selenium 和 Python - SessionNotCreatedException: This version of ChromeDriver only supports Chrome version 84 using ChromeDriver and Chrome through Selenium and Python selenium.common.exceptions.WebDriverException:消息:未知错误:无法使用带有Selenium Python的ChromeDriver Chrome创建Chrome进程 - selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process with ChromeDriver Chrome with Selenium Python 如何通过Chromedriver和Selenium启动Chrome浏览器 - How to start Chrome Browser through Chromedriver and Selenium WebDriverException:消息:未知错误:Chrome 无法启动:通过 VPS 上的 Python 使用 ChromeDriver Chrome 和 Selenium 异常退出 - WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium through Python on VPS 带有 python3、chromedriver、chrome 和 selenium 的 Docker 镜像 - Docker image with python3, chromedriver, chrome & selenium chrome无法使用python硒chromedriver加载 - chrome fails to load with python selenium chromedriver selenium.common.exceptions.WebDriverException: 消息:无法通过 Selenium Python 使用 ChromeDriver Chrome 连接到服务错误 - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python - selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM