Skip to content

Commit

Permalink
AsyncDDGS|DDGS.__del__(): improve logic and add check - if hasattr(se…
Browse files Browse the repository at this point in the history
…lf, "_asession")
  • Loading branch information
deedy5 committed Apr 7, 2024
1 parent d523a4a commit 73a5616
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions duckduckgo_search/duckduckgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def __exit__(
self._close_session()

def __del__(self) -> None:
if self._asession._closed is False:
self._close_session()
self._close_session()

def _close_session(self) -> None:
"""Close the curl-cffi async session."""
self._run_async_in_thread(self._asession.close())
if hasattr(self, "_asession") and self._asession._closed is False:
self._run_async_in_thread(self._asession.close())

def _run_async_in_thread(self, coro: Awaitable[Any]) -> Any:
"""Runs an async coroutine in a separate thread."""
Expand Down
4 changes: 2 additions & 2 deletions duckduckgo_search/duckduckgo_search_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ async def __aexit__(
exc_val: Optional[BaseException] = None,
exc_tb: Optional[TracebackType] = None,
) -> None:
await self._asession.close()
await self._asession.__aexit__(exc_type, exc_val, exc_tb)

def __del__(self) -> None:
if self._asession._closed is False:
if hasattr(self, "_asession") and self._asession._closed is False:
with suppress(RuntimeError, RuntimeWarning):
asyncio.create_task(self._asession.close())

Expand Down

0 comments on commit 73a5616

Please sign in to comment.