2

Hi I try to open parallely multiple instances of Chrome in python using Webdriver and Multiprocessing. After running processes, instances are opening smoothly, but they are not sent to my "instance" array and I can't access instances after that. Please help me, there is my code:

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()
artur_bryt
  • 51
  • 1
  • 3

1 Answers1

0

when the multiprocessing try to pickle the webdriver object, it'll occur some weird error, so instead of passing the object, we can pass the class and build the object inside the new process.

BUT, in that kind of situation, you can not access the driver instances anymore, maybe you can try to send signals to the process.

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