From bb1fa142567fda09d1349609f32552aa9df54afa Mon Sep 17 00:00:00 2001 From: deedy5 <65482418+deedy5@users.noreply.github.com> Date: Mon, 29 Jan 2024 19:22:14 +0300 Subject: [PATCH] Bugfix: add nest_asyncio to run DDGS in an async loop (#182) --- duckduckgo_search/duckduckgo_search.py | 24 ++++++++++-------------- pyproject.toml | 5 +++-- requirements.txt | 3 ++- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/duckduckgo_search/duckduckgo_search.py b/duckduckgo_search/duckduckgo_search.py index 4924473..27dbb6c 100644 --- a/duckduckgo_search/duckduckgo_search.py +++ b/duckduckgo_search/duckduckgo_search.py @@ -1,38 +1,34 @@ import asyncio import logging +import warnings from typing import Dict, Generator, Optional +import nest_asyncio + from .duckduckgo_search_async import AsyncDDGS -from .exceptions import DuckDuckGoSearchException logger = logging.getLogger("duckduckgo_search.DDGS") +nest_asyncio.apply() class DDGS(AsyncDDGS): def __init__(self, headers=None, proxies=None, timeout=10): - self._check_async() + if asyncio.get_event_loop().is_running(): + 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.new_event_loop() + self._loop = asyncio.get_event_loop() def __enter__(self) -> "DDGS": return self 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.") + self._loop.create_task(self.__aexit__(exc_type, exc_val, exc_tb)) - def _iter_over_async(self, ait): + def _iter_over_async(self, async_gen): """Iterate over an async generator.""" - ait = ait.__aiter__() - get_next = ait.__anext__ while True: try: - obj = self._loop.run_until_complete(get_next()) - yield obj + yield self._loop.run_until_complete(async_gen.__anext__()) except StopAsyncIteration: break diff --git a/pyproject.toml b/pyproject.toml index 34b5714..f12a45f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,10 +28,11 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", ] dependencies = [ - "click>=8.1.7", "docstring_inheritance>=2.1.2", + "click>=8.1.7", "curl_cffi>=0.6.0b7", - "lxml>=4.9.3" + "lxml>=4.9.3", + "nest-asyncio>=1.6.0" ] dynamic = ["version"] diff --git a/requirements.txt b/requirements.txt index 265d8e3..db92b47 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ +docstring_inheritance>=2.1.2 click>=8.1.7 curl_cffi>=0.6.0b7 -docstring_inheritance>=2.1.2 lxml>=4.9.3 +nest-asyncio>=1.6.0