Skip to content

Commit

Permalink
Bugfix: add nest_asyncio to run DDGS in an async loop (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 authored Jan 29, 2024
1 parent 3fe1c75 commit bb1fa14
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
24 changes: 10 additions & 14 deletions duckduckgo_search/duckduckgo_search.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bb1fa14

Please sign in to comment.