Skip to content

Commit

Permalink
AsyncDDGS: make _executor class variable
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Mar 12, 2024
1 parent 05b82bd commit 013636a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions duckduckgo_search/duckduckgo_search_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
)

logger = logging.getLogger("duckduckgo_search.AsyncDDGS")
_SHARED_EXECUTOR = ThreadPoolExecutor()


class AsyncDDGS:
"""DuckDuckgo_search async class to get search results from duckduckgo.com."""

_executor = ThreadPoolExecutor()

def __init__(
self,
headers: Optional[Dict[str, str]] = None,
Expand Down Expand Up @@ -247,7 +248,7 @@ async def _text_html_page(s: int, page: int) -> None:
if b"No results." in resp_content:
return

tree = await self._asession.loop.run_in_executor(_SHARED_EXECUTOR, html.document_fromstring, resp_content)
tree = await self._asession.loop.run_in_executor(self._executor, html.document_fromstring, resp_content)

for e in tree.xpath("//div[h2]"):
href = e.xpath("./a/@href")
Expand Down Expand Up @@ -322,7 +323,7 @@ async def _text_lite_page(s: int, page: int) -> None:
if b"No more results." in resp_content:
return

tree = await self._asession.loop.run_in_executor(_SHARED_EXECUTOR, html.document_fromstring, resp_content)
tree = await self._asession.loop.run_in_executor(self._executor, html.document_fromstring, resp_content)

data = zip(cycle(range(1, 5)), tree.xpath("//table[last()]//tr"))
for i, e in data:
Expand Down

0 comments on commit 013636a

Please sign in to comment.