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



我将在一段时间后终止多进程线程,当我终止时,它不会杀死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没有。我想知道当我终止这个过程时,是否有任何选择可以杀死司机。

因为从子代劫持会话的方法似乎不再有效,你不得不使用psutils手动杀死chrome和网络驱动程序及其所有子代,直到他们将会话劫持放入网络驱动程序。

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上的两个子代,但其他系统可能不是这样。

相关内容

  • 没有找到相关文章

最新更新