Skip to content

Commit

Permalink
DDGS: recode using asyncio loop
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Feb 1, 2024
1 parent 8578ca5 commit 1e695d4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions duckduckgo_search/duckduckgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
from .duckduckgo_search_async import AsyncDDGS

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


class DDGS(AsyncDDGS):
def __init__(self, headers=None, proxies=None, timeout=10):
super().__init__(headers, proxies, timeout)
if asyncio.get_event_loop().is_running():
nest_asyncio.apply()
warnings.warn("DDGS running in an async loop. This may cause errors. Use AsyncDDGS instead.", stacklevel=2)
super().__init__(headers, proxies, timeout)
self._loop = asyncio.get_event_loop()
self._loop = asyncio.get_running_loop()
else:
self._loop = asyncio.new_event_loop()
asyncio.set_event_loop(self._loop)

def __enter__(self) -> "DDGS":
return self

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self._loop.create_task(self.__aexit__(exc_type, exc_val, exc_tb))
self._loop.run_until_complete(self.__aexit__(exc_type, exc_val, exc_tb))

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

0 comments on commit 1e695d4

Please sign in to comment.