Skip to content

Commit

Permalink
Merge pull request #1 from ps4vs/drop-ensure-loop-function
Browse files Browse the repository at this point in the history
Remove redundant ensure_event_loop function
  • Loading branch information
lalalune authored Aug 17, 2023
2 parents a896b1c + 49c23eb commit 96a72c2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ print(text)

## API Documentation

### `ensure_event_loop()`

Ensure that there is an event loop in the current thread. If no event loop exists, a new one is created and set for the current thread. This function returns the current event loop.

Example usage:

```python
loop = ensure_event_loop()
```

### `get_browser()`

Get a Playwright browser. If the browser doesn't exist, initializes a new one.
Expand Down
29 changes: 1 addition & 28 deletions agentbrowser/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
context = None


def ensure_event_loop():
"""
Ensure that there is an event loop in the current thread.
If no event loop exists, a new one is created and set for the current thread.
:return: The current event loop.
:rtype: asyncio.AbstractEventLoop
"""
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop


# Synchronous functions


Expand All @@ -37,7 +20,7 @@ def get_browser():
:return: A Playwright browser.
:rtype: playwright.async_api.Browser
"""
ensure_event_loop()
asyncio.get_event_loop()
global browser
if browser is None:
init_browser()
Expand All @@ -53,7 +36,6 @@ def init_browser(headless=True, executable_path=None):
:param executable_path: Path to a Chromium or Chrome executable to run instead of the bundled Chromium.
:type executable_path: str, optional
"""
ensure_event_loop()
asyncio.get_event_loop().run_until_complete(
async_init_browser(headless, executable_path)
)
Expand All @@ -70,7 +52,6 @@ def create_page(site=None):
:return: A new page.
:rtype: playwright.async_api.Page
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_create_page(site))


Expand All @@ -81,7 +62,6 @@ def close_page(page):
:param page: The page to close.
:type page: playwright.async_api.Page
"""
ensure_event_loop()
asyncio.get_event_loop().run_until_complete(async_close_page(page))


Expand All @@ -96,7 +76,6 @@ def navigate_to(url, page, wait_until="domcontentloaded", timeout=10000):
:return: The page after navigation.
:rtype: playwright.async_api.Page
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_navigate_to(url, page, wait_until=wait_until, timeout=timeout))


Expand All @@ -109,7 +88,6 @@ def get_document_html(page):
:return: The HTML content of the page.
:rtype: str
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_get_document_html(page))


Expand All @@ -122,7 +100,6 @@ def get_page_title(page):
:return: The title of the page.
:rtype: str
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_get_page_title(page))


Expand All @@ -135,7 +112,6 @@ def get_body_text(page):
:return: The text content of the page's body.
:rtype: str
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_get_body_text(page))


Expand All @@ -148,7 +124,6 @@ def get_body_html(page):
:return: The HTML content of the page's body.
:rtype: str
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_get_body_html(page))


Expand All @@ -161,7 +136,6 @@ def screenshot_page(page):
:return: A bytes object representing the screenshot.
:rtype: bytes
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(async_screenshot_page(page))


Expand All @@ -175,7 +149,6 @@ def evaluate_javascript(code, page):
:type page: playwright.async_api.Page
:return: The result of the evaluated code.
"""
ensure_event_loop()
return asyncio.get_event_loop().run_until_complete(
async_evaluate_javascript(code, page)
)
Expand Down

0 comments on commit 96a72c2

Please sign in to comment.