ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.96k stars 1.16k forks source link

multithreading uc error #1997

Open dropox1 opened 2 months ago

dropox1 commented 2 months ago

I can't do multithreading, I constantly get errors when

`def main(thread):

seleniumwire_options = {
    'proxy': {
        'http': proxy_url,
        'https': proxy_url,
    }
}

options = uc.ChromeOptions()
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--autoplay-policy=no-user-gesture-required")
options.add_argument("--mute-audio")
options.add_argument("--disable-background-networking")
options.add_argument("--disable-client-side-phishing-detection")
options.add_argument("--disable-sync")
options.add_argument("--disable-component-update")
options.add_argument("--process-per-site")
options.add_argument("--disable-plugins")
options.add_argument("--disable-background-timer-throttling")
options.add_argument("--disable-renderer-backgrounding")
options.add_argument("--disable-features=PreloadMediaEngagementData")
options.add_argument("--enable-low-end-device-mode")
options.add_argument('--ignore-certificate-errors') 
#options.add_argument("--headless=new")

options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

driver = uc.Chrome(seleniumwire_options=seleniumwire_options,
                   options=options,
                   no_sandbox=False,
                   user_multi_procs=True,
                   use_subprocess=False
                   )

  ..............

if name == 'main': mp.set_start_method('spawn', force=True)

p = Pool(processes=2)
p.map(main, range(2))

` error:

Traceback (most recent call last): File "C:\Users\vital\PycharmProjects\test4.py", line 485, in p.map(main, range(2)) File "C:\Users \vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 367, in map return self._map_async(func, iterable, mapstar, chunksize).get() ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 774, in get raise self._value File "C:\Users\vital\AppData\Local\Programs \Python\Python312\Lib\multiprocessing\pool.py", line 125, in worker result = (True, func(*args, kwds)) ^^^^^^^^^^^^^^^^^ ^^^ File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 48, in mapstar return list(map(args)) ^^^^^ ^^^^^^^^^^^ File "C:\Users\vital\PycharmProjects\test4.py", line 94, in main driver = uc.Chrome(seleniumwire_options=seleniumwire_options, ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib \site-packages\seleniumwire\undetected_chromedriver\webdriver.py", line 61, in init super().init(args, kwargs) File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver__init.py", line 258, in init__ self.patcher.auto() File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver\patcher.py", line 138, in auto list(map(lambda f: f.unlink(), files)) File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver\patcher.py", line 138, in list(map(lambda f: f.unlink(), files)) ^^^^^^^^^^ File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 1343, in unlink os.unlink(self) PermissionError: [WinError 5] Permission denied: 'C:\Users\vital\appdata\roaming\undetected_chromedriver\undetected\chromedriver-win32'

and when user_multi_procs=False only one instance of the browser is launched

Deswer60871 commented 2 months ago

I can't do multithreading, I constantly get errors when

`def main(thread):

seleniumwire_options = {
    'proxy': {
        'http': proxy_url,
        'https': proxy_url,
    }
}

options = uc.ChromeOptions()
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--autoplay-policy=no-user-gesture-required")
options.add_argument("--mute-audio")
options.add_argument("--disable-background-networking")
options.add_argument("--disable-client-side-phishing-detection")
options.add_argument("--disable-sync")
options.add_argument("--disable-component-update")
options.add_argument("--process-per-site")
options.add_argument("--disable-plugins")
options.add_argument("--disable-background-timer-throttling")
options.add_argument("--disable-renderer-backgrounding")
options.add_argument("--disable-features=PreloadMediaEngagementData")
options.add_argument("--enable-low-end-device-mode")
options.add_argument('--ignore-certificate-errors') 
#options.add_argument("--headless=new")

options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

driver = uc.Chrome(seleniumwire_options=seleniumwire_options,
                   options=options,
                   no_sandbox=False,
                   user_multi_procs=True,
                   use_subprocess=False
                   )

  ..............

if name == 'main': mp.set_start_method('spawn', force=True)

p = Pool(processes=2)
p.map(main, range(2))

` error:

Traceback (most recent call last): File "C:\Users\vital\PycharmProjects\test4.py", line 485, in p.map(main, range(2)) File "C:\Users \vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 367, in map return self._map_async(func, iterable, mapstar, chunksize).get() ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 774, in get raise self._value File "C:\Users\vital\AppData\Local\Programs \Python\Python312\Lib\multiprocessing\pool.py", line 125, in worker result = (True, func(*args, kwds)) ^^^^^^^^^^^^^^^^^ ^^^ File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 48, in mapstar return list(map(args)) ^^^^^ ^^^^^^^^^^^ File "C:\Users\vital\PycharmProjects\test4.py", line 94, in main driver = uc.Chrome(seleniumwire_options=seleniumwire_options, ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib \site-packages\seleniumwire\undetected_chromedriver\webdriver.py", line 61, in init super().init(args, kwargs) File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriverinit.py", line 258, in init self.patcher.auto() File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver\patcher.py", line 138, in auto list(map(lambda f: f.unlink(), files)) File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver\patcher.py", line 138, in list(map(lambda f: f.unlink(), files)) ^^^^^^^^^^ File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 1343, in unlink os.unlink(self) PermissionError: [WinError 5] Permission denied: 'C:\Users\vital\appdata\roaming\undetected_chromedriver\undetected\chromedriver-win32'

and when user_multi_procs=False only one instance of the browser is launched

I think the problem is somewhere with the profiles, I did it so that I created more profiles and made a special function for them to start and it worked

dropox1 commented 2 months ago

Я не могу выполнять многопоточность, я постоянно получаю ошибки, когда `def main(поток):

seleniumwire_options = {
    'proxy': {
        'http': proxy_url,
        'https': proxy_url,
    }
}

options = uc.ChromeOptions()
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--autoplay-policy=no-user-gesture-required")
options.add_argument("--mute-audio")
options.add_argument("--disable-background-networking")
options.add_argument("--disable-client-side-phishing-detection")
options.add_argument("--disable-sync")
options.add_argument("--disable-component-update")
options.add_argument("--process-per-site")
options.add_argument("--disable-plugins")
options.add_argument("--disable-background-timer-throttling")
options.add_argument("--disable-renderer-backgrounding")
options.add_argument("--disable-features=PreloadMediaEngagementData")
options.add_argument("--enable-low-end-device-mode")
options.add_argument('--ignore-certificate-errors') 
#options.add_argument("--headless=new")

options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

driver = uc.Chrome(seleniumwire_options=seleniumwire_options,
                   options=options,
                   no_sandbox=False,
                   user_multi_procs=True,
                   use_subprocess=False
                   )

  ..............

if name == 'main': mp.set_start_method('spawn', force=True)

p = Pool(processes=2)
p.map(main, range(2))

` error: Traceback (most recent call last): File "C:\Users\vital\PycharmProjects\test4.py", line 485, in p.map(main, range(2)) File "C:\Users \vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 367, in map return self._map_async(func, iterable, mapstar, chunksize).get() ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 774, in get raise self._value File "C:\Users\vital\AppData\Local\Programs \Python\Python312\Lib\multiprocessing\pool.py", line 125, in worker result = (True, func(*args, kwds)) ^^^^^^^^^^^^^^^^^ ^^^ File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\pool.py", line 48, in mapstar return list(map(args)) ^^^^^ ^^^^^^^^^^^ File "C:\Users\vital\PycharmProjects\test4.py", line 94, in main driver = uc.Chrome(seleniumwire_options=seleniumwire_options, ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib \site-packages\seleniumwire\undetected_chromedriver\webdriver.py", line 61, in init super().init(args, kwargs) File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriverinit.py", line 258, in init self.patcher.auto() File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver\patcher.py", line 138, in auto list(map(lambda f: f.unlink(), files)) File "C:\Users\vital\PycharmProjects\PycharmProjects\Lib\site-packages\undetected_chromedriver\patcher.py", line 138, in list(map(lambda f: f.unlink(), files)) ^^^^^^^^^^ File "C:\Users\vital\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 1343, in unlink os.unlink(self) PermissionError: [WinError 5] Permission denied: 'C:\Users\vital\appdata\roaming\undetected_chromedriver\undetected\chromedriver-win32' and when user_multi_procs=False only one instance of the browser is launched

I think the problem is somewhere with the profiles, I did it so that I created more profiles and made a special function for them to start and it worked

thanks, I'll try that later

boludoz commented 2 months ago

It seems that it’s not possible because it uses the same location for the modified chromedriver. Perhaps you could patch it yourself and choose a custom location. I recommend using nodriver; it’s worth the transition. Multithreading is not possible in undetected_chromedriver.

I use it with nodriver, running 2 to 3 drivers at the same time, each with its own daemon, and I haven’t had any problems. Each one takes care of its own core.

https://github.com/ultrafunkamsterdam/nodriver/

import asyncio
import threading
import nodriver as uc

async def procesar_pedidos(string : str):
    print(f"Procesando {string}")
    browser = await uc.start()
    await asyncio.sleep(3)
    await browser.get(f'https://{string}')
    await asyncio.sleep(3)

def start_event_loop(coro):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(coro)

async def main():
    coros_str = ['www.twitter.com', 'www.google.com']
    coros = [procesar_pedidos(string) for string in coros_str]
    threads = [threading.Thread(target=start_event_loop, args=(coro,)) for coro in coros]

    for thread in threads:
        thread.start()

    try:
        while True:
            await asyncio.sleep(1)
    except KeyboardInterrupt:
        print("Programa finished")

if __name__ == '__main__':
    asyncio.run(main())

And please, avoid touch this options:

                   no_sandbox=False,
                   user_multi_procs=True,
                   use_subprocess=False