Skip to content

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

Closed
@mdmintz

Description

@mdmintz

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.

Metadata

Metadata

Assignees

Labels

UC Mode / CDP ModeUndetected Chromedriver Mode / CDP ModebugUh oh... Something needs to be fixed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions