如何用 selenium 铬驱动程序杀死多进程

p4tfgftt  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(190)

我在一段时间后终止多进程线程,当我终止它时,它不会杀死chromeDriver。以下是代码的一个示例(保持简单):

def start_thread(url):
    driver = webdriver.Chrome()
    driver.get(url)

urls=[]
threads=[]
for counter in range(urls):
    thread = multiprocessing.Process( target=start_thread,name=f'start_thread_{counter}', args=[url])
    threads.append(thread)
    thread.start()

sleep(10)
for t in threads:
    t.terminate()

线程确实被终止了,但chromeDriver并非如此。我想知道在终止进程时是否有任何选项可以终止该驱动程序。

ymdaylpp

ymdaylpp1#

因为从孩子那里劫持会话的方法似乎不再起作用,你不得不使用psutils手动终止Chrome和WebDriver及其所有子项,直到他们将会话劫持添加到Web驱动程序中。

import time
import multiprocessing
from selenium import webdriver
import psutil

def start_thread(url):
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(30)

def get_all_children(proc: psutil.Process):
    try:
        if len(proc.children()) == 0:
            return []
        else:
            returned_list = []
            for item in proc.children():
                returned_list.append(item)
                returned_list.extend(get_all_children(item))
            return returned_list
    except psutil.NoSuchProcess:
        return []

if __name__ == "__main__":
    urls = ['http://www.python.org','http://www.python.org']
    threads = []
    for counter in range(len(urls)):
        thread = multiprocessing.Process(target=start_thread, name=f'start_thread_{counter}', args=(urls[counter],))
        threads.append(thread)
        thread.start()

    time.sleep(5)
    for t in threads:
        children = get_all_children(psutil.Process(pid=t.pid))
        t.terminate()
        for child in children:
            try:
                child.terminate()
            except psutil.NoSuchProcess:
                pass  # in case child was already dead

        time.sleep(5)  # to see what's happening

以这种方式搜索孩子在某种程度上是必要的,因为Chrome往往会打开很多孩子,尽管它可能仅限于Windows上的两个孩子,但其他系统可能不是这样。

相关问题