Skip to content

"There is no current event loop in thread 'ThreadPoolExecutor-0_0'." in cdp_util.start_sync() (when multithreaded) #3479

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

Closed
mdmintz opened this issue Feb 5, 2025 · 1 comment · Fixed by #3481
Assignees
Labels
bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@mdmintz
Copy link
Member

mdmintz commented Feb 5, 2025

There is no current event loop in thread 'ThreadPoolExecutor-0_0'. in cdp_util.start_sync() (when multithreaded)


I was working on a new multithreading example for the SB CDP API when I encountered the error from:

import asyncio
from concurrent.futures import ThreadPoolExecutor
from random import randint
from seleniumbase.core import sb_cdp
from seleniumbase.undetected import cdp_driver

def main(url):
    loop = asyncio.new_event_loop()
    driver = cdp_driver.cdp_util.start_sync()
    page = loop.run_until_complete(driver.get(url))
    sb = sb_cdp.CDPMethods(loop, page, driver)
    sb.set_window_rect(randint(4, 720), randint(8, 410), 800, 500)
    sb.press_keys("input", "Text")
    sb.highlight("button")
    sb.click("button")
    sb.sleep(2)

if __name__ == "__main__":
    urls = ["https://seleniumbase.io/demo_page" for i in range(4)]
    with ThreadPoolExecutor(max_workers=len(urls)) as executor:
        for url in urls:
            executor.submit(main, url)

That caused the script to end early. (Multithreading masked the error from the console output.)

The logic in cdp_util.start_sync() should be updated:

  • Accept a loop kwarg to use an existing event loop.
  • If no "loop" is set, then use asyncio.new_event_loop() instead of asyncio.get_event_loop() if RuntimeError.
@mdmintz mdmintz added bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels Feb 5, 2025
@mdmintz mdmintz self-assigned this Feb 5, 2025
@mdmintz
Copy link
Member Author

mdmintz commented Feb 6, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Uh oh... Something needs to be fixed UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant