Vinyzu / Botright

Botright, the most advance undetected, fingerprint-changing, captcha-solving, open-source automation framework. Build on Playwright, its as easy to use as it is to extend your code. Solving your Captchas for free with AI.
GNU General Public License v3.0
424 stars 57 forks source link

Is there any example to use mutiple botright browser asynchronously? [Question] #63

Open 201411164 opened 6 months ago

201411164 commented 6 months ago

Currently, If I run 5 multiple browsers at the same time, if a first browser is finished and tries to run a new one while others are running, it says "the other process is using so it can't open a new browser" How can I fix this issue?

Vinyzu commented 6 months ago

Can you please provide a code snippet for this issue

201411164 commented 6 months ago

async def init_botright(): botright_client = None try: botright_client = await botright.Botright() proxy_use_yn = 1

    if proxy_use_yn == 1:
        proxy_id, proxy_password = await generate_proxy_credentials()
        endpoint = 'my endpoint'
        port = 'my port'
        proxy_url = f"{proxy_id}:{proxy_password}@{endpoint}:{port}"            
        browser = await botright_client.new_browser(proxy=proxy_url)
    else:            
        browser = await botright_client.new_browser()

    page = await browser.new_page()
    return page, browser, botright_client
except Exception as e:
    print(f"Error in init_botright: {e}")
    return None, None, None

async def main(account):

page, context, botright_client = await init_botright()
if page is None or context is None or botright_client is None:
    print(f"Failed to initialize botright for account {account[0]}, skipping...")

try:      
    my code here
    pass
except Exception as e:
    print(f"Error processing account {account[0]}: {e}")
finally:
    if context:
        await context.close()
    if botright_client:
        await botright_client.close()

async def run_accounts(accounts, max_run_cnt): semaphore = asyncio.Semaphore(max_run_cnt)

async def run_with_semaphore(account):
    try:
        async with semaphore:
            await main(account)
    except Exception as e:
        print(f"Error running account {account[0]} with semaphore: {e}")
        await asyncio.sleep(10)

tasks = [run_with_semaphore(account) for account in accounts]
await asyncio.gather(*tasks, return_exceptions=True)

I execute it by like this asyncio.run(run_accounts(accounts, max_run_cnt))

201411164 commented 6 months ago

-----Original Message----- From: @.> To: @.>; Cc: @.>; @.>; Sent: 2024-02-28 (수) 15:26:55 (GMT+09:00) Subject: Re: [Vinyzu/Botright] Is there any example to use mutiple botright browser asynchronously? [Question] (Issue #63)

Can you please provide a code snippet for this issue — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

Hi, I left the code snippet for this. Thank you very much!Message ID: @.***>

201411164 commented 6 months ago

'C:\Users\82108\AppData\Local\Temp\botright-nfseect3\Default\Affiliation Database' This was the error message

GandaG commented 6 months ago

I'm getting the exact same issue, here's my full traceback:

  Traceback (most recent call last):
    File "C:\Users\danie\Documents\Projects\TrafficMaster\main.py", line 77, in <module>
      asyncio.run(main())
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
      return runner.run(main)
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
      return self._loop.run_until_complete(task)
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 640, in run_until_complete
      self.run_forever()
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\windows_events.py", line 321, in run_forever
      super().run_forever()
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 607, in run_forever
      self._run_once()
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 1922, in _run_once
      handle._run()
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\asyncio\events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
  > File "C:\Users\danie\Documents\Projects\TrafficMaster\main.py", line 27, in worker
      await task(task_id)
    File "C:\Users\danie\Documents\Projects\TrafficMaster\camfyre.py", line 111, in task
      botright_client = await botright.Botright(headless=False)
    File "C:\Users\danie\Documents\Projects\TrafficMaster\.venv\Lib\site-packages\async_class.py", line 173, in __await__
      yield from self.create_task(
    File "C:\Users\danie\Documents\Projects\TrafficMaster\.venv\Lib\site-packages\botright\botright.py", line 91, in __ainit__
      self.delete_botright_temp_dirs()
    File "C:\Users\danie\Documents\Projects\TrafficMaster\.venv\Lib\site-packages\botright\botright.py", line 226, in delete_botright_temp_dirs
      shutil.rmtree(os.path.join(temp_path, temp_dir))
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 759, in rmtree
      return _rmtree_unsafe(path, onerror)
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 617, in _rmtree_unsafe
      _rmtree_unsafe(fullname, onerror)
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 622, in _rmtree_unsafe
      onerror(os.unlink, fullname, sys.exc_info())
    File "C:\Users\danie\AppData\Local\Programs\Python\Python311\Lib\shutil.py", line 620, in _rmtree_unsafe
      os.unlink(fullname)
  PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\danie\\AppData\\Local\\Temp\\botright-j9b55tbo\\Default\\Affiliation Database'
xxxyo commented 1 month ago

hi,I have also encountered the same problem, which seems to be caused by reading and writing in the same directory when starting the browser configuration. Do you know how to solve this problem? I do need to start the browser asynchronously, please contact me if possible!

GJayLG commented 1 month ago

same problem