Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
201411164 opened this issue Feb 28, 2024 · 5 comments
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested

Comments

@201411164
Copy link

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?

@201411164 201411164 added documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested labels Feb 28, 2024
@Vinyzu
Copy link
Owner

Vinyzu commented Feb 28, 2024

Can you please provide a code snippet for this issue

@201411164
Copy link
Author

201411164 commented Feb 28, 2024

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
Copy link
Author

201411164 commented Feb 28, 2024 via email

@201411164
Copy link
Author

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

@GandaG
Copy link
Contributor

GandaG commented Mar 2, 2024

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants