Multiprocessing, python - sharing the same webdriver pointer

Viewed 208

I couldn't find a proper response so I post this question.

The fastest way to understand the question is the goal:

There is a main process and a subprocess (the one I want to create). The main process inspects several websites via webdriver, but sometimes it got stuck at low selenium level and don't want to change the official code. So.. I manually inspect sometimes the monitor to the check whether the process got stuck, and if so, then I change manually the url in the browser and it works again smooth. I don't want to be a human checker.. so i'd like to automate the task with a subprocess that shares the same webdriver and inspects the url by webdriver.current_url and do the work for me.

Here is my try in the minimal representative example form in which the sub-process only detects a change in the url of the webdriver

def test_sub(driver):

    str_site0 = driver.current_url  # get the site0 url

    time.sleep(4)                   # give some time to the main-process to change to site1
    str_site1 = driver.current_url  # get the site1 url (changed by main-process)

    if str_site0 == str_site1:
        print('sub: no change detected')
    else:
        print('sub: change detected')
    #endif

#enddef sub



def test_main():
    """ main process changes from site0 (stackoverflow) to site1 (youtube)
        sub process detects this change of url of the webdriver object (same pointer) by using 
        ".current_url" method
    """

    # init driver
    pat_webdriver   = r"E:\WPy64-3680\python-3.6.8.amd64\Lib\site-packages\selenium\v83_chromedriver\chromedriver.exe"
    driver          = webdriver.Chrome(executable_path= pat_webdriver)
    time.sleep(2)


    # open initial site
    str_site0 = 'https://stackoverflow.com'
    driver.get(str_site0)
    time.sleep(2)


    # init sub and try to pass the webdriver object
    p = multiprocessing.Process(target=test_sub, args=(driver,)) # PROBLEM HERE! PYTHON UNCAPABLE
    p.daemon = False
    p.start()


    # change site
    time.sleep(0.5)                   # give some time sub query webdriver with site0
    str_site1 = 'https://youtube.com' # site 1 (this needs to be detected by sub)
    driver.get(str_site1)


    # wait the sub to detect the change in url. and kill process (non-daemon insufficient don't know why..)
    time.sleep(3)
    p.terminate()

#enddef test_main


# init the program (main-process)
test_main()

the corresponding error by executing $python test_multithread.py (it's the name of the test script..) is the following one: enter image description here

0 Answers
Related