Hi我尝试使用Webdriver和Multiprocessing在python中并行打开多个Chrome实例。运行进程后,实例会顺利打开,但它们不会发送到我的"实例"数组,之后我无法访问实例。请帮帮我,这是我的代码:
from selenium import webdriver
from multiprocessing import Process
import time
num = 3
process = [None] * num
instance = [None] * num
def get():
for i in range(num):
try:
instance[i].get("https://www.youtube.com")
except:
print("Can't connect to the driver")
time.sleep(1)
get()
def create_instance(i):
instance[i] = webdriver.Chrome()
if __name__ == '__main__':
for i in range(num):
process[i] = Process(target = create_instance, args = [i])
process[i].start()
for i in range(num):
process[i].join()
get()
当multiprocessing
尝试pickle webdriver对象时,会出现一些奇怪的错误,所以我们可以在新进程中传递类并构建对象,而不是传递对象。
但是,在这种情况下,您无法再访问驱动程序实例,也许您可以尝试向进程发送信号。
from selenium import webdriver
from multiprocessing import Process
import time
num = 3
process = [None] * num
def get(id, Driver):
driver = Driver()
driver.get(f"https://www.google.com?id={id}")
time.sleep(10)
driver.close()
if __name__ == '__main__':
for i in range(num):
process[i] = Process(target=get, args = [i, webdriver.Chrome])
process[i].start()
for i in range(num):
process[i].join()