Skip to content

Commit

Permalink
raise exception if DDGS is used in async code
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Jan 28, 2024
1 parent 7548dbb commit 3828148
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions duckduckgo_search/duckduckgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from typing import Dict, Generator, Optional

from .duckduckgo_search_async import AsyncDDGS
from .exceptions import DuckDuckGoSearchException

logger = logging.getLogger("duckduckgo_search.DDGS")


class DDGS(AsyncDDGS):
def __init__(self, headers=None, proxies=None, timeout=10):
self._check_async()
super().__init__(headers, proxies, timeout)
self._loop = asyncio.new_event_loop()

Expand All @@ -18,6 +20,11 @@ def __enter__(self) -> "DDGS":
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self._loop.run_until_complete(self.__aexit__(exc_type, exc_val, exc_tb))

def _check_async(self):
"""Raises an exception if DDGS is used in async code."""
if asyncio.get_event_loop().is_running():
raise DuckDuckGoSearchException("DDGS is not compatible with async code. Use AsyncDDGS instead.")

def _iter_over_async(self, ait):
"""Iterate over an async generator."""
ait = ait.__aiter__()
Expand Down

0 comments on commit 3828148

Please sign in to comment.